You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/versioning.md
+53-5Lines changed: 53 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,11 +7,11 @@ A new version of your library is source compatible with a previous version if co
7
7
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.
8
8
9
9
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.
11
11
12
12
## virtual
13
13
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.
15
15
As a library developer you might want to do this for members that could benefit from a custom implementation in a consuming application.
16
16
17
17
Take the following example:
@@ -142,8 +142,8 @@ Area of Circle = 154
142
142
```
143
143
144
144
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,
147
147
making a call to the original area method implementation is as simple as calling `base.Area()` in a derived class.
148
148
149
149
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
152
152
* Making a previously virtual method non-virtual is neither source nor binary compatible. It will cause errors during compilation and will break existing applications.
153
153
* You cannot use the `override` modifier with `static`, `abstract`, `virtual` or `new` modifiers.
154
154
155
+
155
156
## new
156
157
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
+
publicclassBaseClass
165
+
{
166
+
publicvoidMethod1()
167
+
{
168
+
Console.WriteLine("A base method");
169
+
}
170
+
}
171
+
172
+
publicclassDerivedClass : BaseClass
173
+
{
174
+
newpublicvoidMethod1()
175
+
{
176
+
Console.WriteLine("A derived method");
177
+
}
178
+
}
179
+
180
+
publicstaticvoidMain()
181
+
{
182
+
BaseClassb=newBaseClass();
183
+
DerivedClassd=newDerivedClass(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