Skip to content

Commit ecc0430

Browse files
committed
flesh out content for new modifier
1 parent 5022a13 commit ecc0430

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

docs/csharp/versioning.md

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ A new version of your library is source compatible with a previous version if co
77
In contrast, a new version of your library is binary compatible if an application that depended on the old version can, without recompilation, work with the new version.
88

99
Lucky for you C# has features to ensure that as a class evolves over time, through the addition of new members, other classes that derive from it still keep working as intended.
10-
This is achieved through the use of the `virtual`, `override` and `new` keywords.
10+
This is achieved through the use of the `virtual`, `override` and `new` modifiers.
1111

1212
## virtual
1313

14-
You use the virtual keyword to indicate a method or property declaration whose implementation you want to be overridden in a derived class.
14+
You use the virtual modifier to indicate a method or property declaration whose implementation you want to be overridden in a derived class.
1515
As a library developer you might want to do this for members that could benefit from a custom implementation in a consuming application.
1616

1717
Take the following example:
@@ -142,8 +142,8 @@ Area of Circle = 154
142142
```
143143

144144
From the sample code above you'll notice there's a base class `Shape` from which `Square` and `Circle` inherit.
145-
The `Circle` class provides its own implementation for the `Area` method by using the `override` keyword.
146-
Original method implementations can still be accessed on the `base` keyword, notice how the `Square` class uses the constructor of the base class,
145+
The `Circle` class provides its own implementation for the `Area` method by using the `override` modifier.
146+
Original method implementations can still be accessed on the `base` modifier, notice how the `Square` class uses the constructor of the base class,
147147
making a call to the original area method implementation is as simple as calling `base.Area()` in a derived class.
148148

149149
When building or consuming a library here are some things to note about the override modifier:
@@ -152,6 +152,54 @@ When building or consuming a library here are some things to note about the over
152152
* Making a previously virtual method non-virtual is neither source nor binary compatible. It will cause errors during compilation and will break existing applications.
153153
* You cannot use the `override` modifier with `static`, `abstract`, `virtual` or `new` modifiers.
154154

155+
155156
## new
156157

157-
_*TODO*: Explain new keyword with code sample, explain how it ties into previous sample code_
158+
You might run into situations where you'd like to completely hide a member inherited from a base class,
159+
one example that comes to mind is when you'd like to provide your own implementation for a non-virtual method which can't be overriden.
160+
161+
Take the following example:
162+
163+
```csharp
164+
public class BaseClass
165+
{
166+
public void Method1()
167+
{
168+
Console.WriteLine("A base method");
169+
}
170+
}
171+
172+
public class DerivedClass : BaseClass
173+
{
174+
new public void Method1()
175+
{
176+
Console.WriteLine("A derived method");
177+
}
178+
}
179+
180+
public static void Main()
181+
{
182+
BaseClass b = new BaseClass();
183+
DerivedClass d = new DerivedClass(7);
184+
185+
Console.WriteLine(b.Method1());
186+
Console.WriteLine(b.Method1());
187+
}
188+
```
189+
190+
#### Output
191+
192+
```
193+
A base method
194+
A derived method
195+
```
196+
197+
In this example you hide the `Method1` method from the base class in the derived class.
198+
If the `new` modifier is not included a compiler warning will be generated stating that you're hiding an inherited member,
199+
a compiler warning will also be generated if the `new` modifier is used with a method that doesn't hide an inherited member.
200+
201+
When building or consuming a library here are some things to note about the `new` modifier:
202+
203+
* Since the program still compiles despite compiler warnings the removal of a hidden method in the base class is both source and binary compatible.
204+
* Adding a new method to your library that shares the same signature with a method in the derived class will only cause compiler warnings as your method will be hidden by default.
205+
* You cannot use the `new` modifier with the `override` modifier.

0 commit comments

Comments
 (0)