tags:

views:

168

answers:

2

When we have new in C#, that personally I see only as a workaround to override a property that does not have a virtual/overridable declaration, in VB.NET we have two "concepts" Shadows and Overloads.

In which case prefer one to another?

A: 

Shadows is for cases where your base class is Function SomeMethod() As String and you want to have Function SomeMethod() As Integer. Basically, to change the return type.

Overloads is for case where your base class is Function SomeMethod() As String and you want to add a parameter such as Function SomeMethod(ByVal value As Integer) As String.

Daniel A. White
**Overloads** can also be used to **Shadow** an existing member, or set of overloaded members, in a base class. When you use Overloads in this way, you declare the property or method with the same name and the same argument list as the base class member, and you do not supply the Shadows keyword.
serhio
+2  A: 

There are three closely related concepts; overriding, shadowing and overloading.

Overriding is when you make a new implementation for a virtual method.

Shadowing is when you make a new non-virtual implementation for a method.

Overloading is when you add a method with the same name but different parameters.

All three concepts are available both in C# and VB.

Guffa
The question is not about overriding, this is OK. Now **Overloads** can also be used to **shadow** an existing member, or set of overloaded members, in a base class. When you use Overloads in this way, you declare the property or method with the same name and the same argument list as the base class member, and you do not supply the Shadows keyword. I see *new* (C#) = *Shadows* (VB.NET), what is the C# equivalent to overloads?
serhio
@serhio: I just mentioned overriding for completeness because it's a closely related concept. As the Overloads keyword in VB can also be used for shadowing, there is no exact equivalent in C#. In C# there is no keyword for overloading, you just declare a method with the same name and different signature to overload it.
Guffa
so, your definitions is not complete, so these 2 concepts remains unclear for me. `Shadows` creates a "new" function, and `Overloads` is (can be) used for the same scope. so what the difference.
serhio
@serhio: Well, the definitions of the concepts is pretty complete (while there is of course a lot more that you can explain about them). The concepts doesn't map 1-to-1 to the VB keywords, so while you sometimes can use the `Overloads` keyword to to shadowing it's still shadowing that you do. The concepts remain the same even when the VB keywords overlap.
Guffa
I agree. However in practice I use keywords not concepts, and if I have the same(non-virtual) method with the same parameters in the child class, VS warns me to use the "Overloads" keyword, but not the "Shadows" one, like it should result from the concept that you described.
serhio
@serhio: Well, actually you use both the keyword and the concept. :) Although the `Overloads` keyword seems to be useable for most situations where you can use the `Shadows` keyword, I would recommend that you use the keyword that corresponds to the actual concept that you are using, that makes the code easier to read.
Guffa
@serhio: The warnings that you get from the compiler usually suggests what works in most situations, not neccesarily what fits best.
Guffa
@Guffa: "fits best"... for me, both Shadows and Overloads fits good. Finally, if the result is the same, that is no sense to have both. Oh... what why I prefer C#: no strange ambiguity, no necessity to "be compatible" with the archaic syntax... Thanks, Guffa. Let's close this topic and go on.
serhio