Sorry for the long question, but I'm new to C # (I used VB.Net)
I fully understand the difference between Overriding and Overloading in both VB.Net and C # .. so there is no problem with overriding.
Now in VB.Net there is a difference between Shadowing (using the Shadows keyword) and Overload (using the Overloads keyword) with the same arguments ") as follows:
- When using Shadow, it shades every method with the same name - regardless of the arguments - for only one method (shadow method).
- When using Overload - with the same arguments, it overloads (shadows) only the method with the same name and arguments.
Consider this code:
Class A
Sub MyMethod()
Console.WriteLine("A.MyMethod")
End Sub
Sub MyMethod(ByVal x As Integer)
Console.WriteLine("A.MyMethod (x)")
End Sub
Sub MyMethod2()
Console.WriteLine("A.MyMethod2")
End Sub
Sub MyMethod2(ByVal x As Integer)
Console.WriteLine("A.MyMethod2 (x)")
End Sub
End Class
Class B
Inherits A
Overloads Sub MyMethod()
Console.WriteLine("B.MyMethod")
End Sub
Shadows Sub MyMethod2()
Console.WriteLine("B.MyMethod2")
End Sub
End Class
Then:
Dim obj As New B()
obj.MyMethod() 'B.MyMethod
obj.MyMethod(10) 'A.MyMethod (x)
obj.MyMethod2() 'B.MyMethod2
While:
obj.MyMethod2(10) 'Error, cuz there only one 'MyMethod2' (and with zero arguments)
So far so good ..
BUT, in C # I don't get the difference between Shadowing (using the New keyword) and Overload (same name and arguments)!
So, when trying to use the same code above in C # (using C # syntax, of course : D ), the following line:
obj.MyMethod2(10);
will return → 'A.MyMethod2 (x)'
There seems to be no difference between overload and shadow in C #!
Can anyone explain why this discrepancy exists?
thanks