Skip to content

Commit badd9fe

Browse files
CopilotBillWagnergewarren
authored
Clarify method hiding explanation in polymorphism documentation (#47378)
* Initial plan * Clarify method hiding explanation in polymorphism documentation Co-authored-by: BillWagner <[email protected]> * Fix hyphenation: change 'runtime' to 'run-time' for consistency Co-authored-by: gewarren <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: gewarren <[email protected]>
1 parent 2680077 commit badd9fe

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

docs/csharp/fundamentals/object-oriented/polymorphism.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,14 @@ If you want your derived class to have a member with the same name as a member i
5454

5555
:::code language="csharp" source="./snippets/inheritance/Inheritance.cs" ID="SnippetNewMethods":::
5656

57-
Hidden base class members may be accessed from client code by casting the instance of the derived class to an instance of the base class. For example:
57+
When you use the `new` keyword, you're creating a method that *hides* the base class method rather than *overriding* it. This is different from virtual methods. With method hiding, the method that gets called depends on the compile-time type of the variable, not the run-time type of the object.
58+
59+
Hidden base class members can be accessed from client code by casting the instance of the derived class to an instance of the base class. For example:
5860

5961
:::code language="csharp" source="./snippets/inheritance/Inheritance.cs" ID="SnippetUseNewMethods":::
6062

63+
In this example, both variables refer to the same object instance, but the method that gets called depends on the variable's declared type: `DerivedClass.DoWork()` when accessed through the `DerivedClass` variable, and `BaseClass.DoWork()` when accessed through the `BaseClass` variable.
64+
6165
### Prevent derived classes from overriding virtual members
6266

6367
Virtual members remain virtual, regardless of how many classes have been declared between the virtual member and the class that originally declared it. If class `A` declares a virtual member, and class `B` derives from `A`, and class `C` derives from `B`, class `C` inherits the virtual member, and may override it, regardless of whether class `B` declared an override for that member. The following code provides an example:

0 commit comments

Comments
 (0)