Skip to content

Commit 7d1f176

Browse files
add stages in article (#47697)
1 parent dea054a commit 7d1f176

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

docs/csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ dc.Method2();
8383
bcdc.Method1();
8484
bcdc.Method2();
8585
```
86-
87-
When you build the project, you see that the addition of the `Method2` method in `BaseClass` causes a warning. The warning says that the `Method2` method in `DerivedClass` hides the `Method2` method in `BaseClass`. You are advised to use the `new` keyword in the `Method2` definition if you intend to cause that result. Alternatively, you could rename one of the `Method2` methods to resolve the warning, but that is not always practical.
86+
87+
## The New Keyword
88+
89+
When you build the project, you see that the addition of the `Method2` method in `BaseClass` causes a warning. The warning says that the `Method2` method in `DerivedClass` hides the `Method2` method in `BaseClass`. You are advised to use the `new` keyword in the `Method2` definition if you intend to cause that result. Alternatively, you could rename one of the `Method2` methods to resolve the warning, but that is not always practical.
8890

8991
Before adding `new`, run the program to see the output produced by the additional calling statements. The following results are displayed.
9092

@@ -111,7 +113,9 @@ public new void Method2()
111113

112114
Run the program again to verify that the output has not changed. Also verify that the warning no longer appears. By using `new`, you are asserting that you are aware that the member that it modifies hides a member that is inherited from the base class. For more information about name hiding through inheritance, see [new Modifier](../../language-reference/keywords/new-modifier.md).
113115

114-
To contrast this behavior to the effects of using `override`, add the following method to `DerivedClass`. The `override` modifier can be added before or after `public`.
116+
## Virtual and Override Keywords
117+
118+
To contrast this behavior to the effects of using `override`, add the following method to `DerivedClass`. The `override` modifier can be added before or after `public`.
115119

116120
```csharp
117121
public override void Method1()
@@ -212,8 +216,10 @@ namespace OverrideAndNew
212216
}
213217
}
214218
```
215-
216-
The following example illustrates similar behavior in a different context. The example defines three classes: a base class named `Car` and two classes that are derived from it, `ConvertibleCar` and `Minivan`. The base class contains a `DescribeCar` method. The method displays a basic description of a car, and then calls `ShowDetails` to provide additional information. Each of the three classes defines a `ShowDetails` method. The `new` modifier is used to define `ShowDetails` in the `ConvertibleCar` class. The `override` modifier is used to define `ShowDetails` in the `Minivan` class.
219+
220+
## Override and New in Derived Classes
221+
222+
The following example illustrates similar behavior in a different context. The example defines three classes: a base class named `Car` and two classes that are derived from it, `ConvertibleCar` and `Minivan`. The base class contains a `DescribeCar` method. The method displays a basic description of a car, and then calls `ShowDetails` to provide additional information. Each of the three classes defines a `ShowDetails` method. The `new` modifier is used to define `ShowDetails` in the `ConvertibleCar` class. The `override` modifier is used to define `ShowDetails` in the `Minivan` class.
217223

218224
```csharp
219225
// Define the base class, Car. The class defines two methods,

0 commit comments

Comments
 (0)