C # vs VB.Net, what is the difference between Shadowing and [Overloading (without changing arguments)]

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

+4
5

. .

?

, . ; , , .

? ?

. . , , .

#, ?

, , . ( , , .)

D, , M (X) M (Y), , , B, , D. .

#, ?

, : . , . .

, ; . ( , , , .)

, . , , , ; , , . , .

, -, .

#, ?

M (X) , , . .

, , , , , , . , , - , , .

# ?

; , . # , ; . , new . , .

#, , , ?

. , . , , , , , .

, VB, #?

.

?

# VB, , . , . , .

#?

. , . , .

+9

# VB IL, . # VB, VB #.

New # "". .

+3

this

#, , . , ToBeShadowed ToBeShadowed() . , , , , . , .

public class A {
    public int ToBeShadowed; //This is a field in the parent class
}

public class B : A {
    public void ToBeShadowed(){ //this is a method in the child
        //do something
    }
}

, , . , . , . , . Shadowing .

public class A { //This is overloading
    public int ToBeOverloaded(); //with no input
    public int ToBeOverloaded(int input); //with 1 input, integer
    public int ToBeOverloaded(string input); //with 1 input, string
}
+2

Overload - , .

Shadowing (Shadows VB - New #) - . . , .

New Shadows . , VB, , .

:

Module StartupModule

    Sub Main()
        Dim l1 As Level1 = New Level3
        l1.Print()
        l1.Print(10)

        Console.WriteLine()

        Dim l2 As Level2 = New Level3
        l2.Print()
        l2.Print("20")

        Console.WriteLine()

        Dim l3 As Level3 = New Level3
        l3.Print()
        l3.Print(DateTime.Now)

        Console.ReadLine()
    End Sub


    Public Class Level1
        Public Overloads Sub Print()
            Console.WriteLine("Level1.Print")
        End Sub

        Public Overloads Sub Print(value As Integer)
            Console.WriteLine("Level1.Print(Value)={0}", value)
        End Sub
    End Class

    Public Class Level2
        Inherits Level1

        Public Shadows Sub Print()
            Console.WriteLine("Level2.Print")
        End Sub

        Public Shadows Sub Print(value As String)
            Console.WriteLine("Level2.Print(Value)={0}", value & "1")
        End Sub
    End Class

    Public Class Level3
        Inherits Level2

        Public Shadows Sub Print()
            MyBase.Print()
        End Sub

        Public Shadows Sub Print(value As DateTime)
            Console.WriteLine("Level3.Print(Value)={0}", value)
        End Sub
    End Class

End Module

- , , .

Class A
    Sub MyMethod()
        Console.WriteLine("A.MyMethod")
    End Sub
    ...
End Class

Class B
    Inherits A
    Overloads Sub MyMethod()
        Console.WriteLine("B.MyMethod")
    End Sub
    ...
End Class

, MyMethod Overloads Class B, Shadows. :

Module StartupModule

    Sub Main()
        Dim l1 As Level1 = New Level3
        l1.Print()

        Console.WriteLine()

        Dim l2 As Level2 = New Level3
        l2.Print()

        Console.WriteLine()

        Dim l3 As Level3 = New Level3
        l3.Print()

        Console.ReadLine()
    End Sub


    Public Class Level1
        Public Sub Print()
            Console.WriteLine("Level1.Print")
        End Sub
    End Class

    Public Class Level2
        Inherits Level1

        Public Overloads Sub Print()
            Console.WriteLine("Level2.Print")
        End Sub
    End Class

    Public Class Level3
        Inherits Level2

        Public Overloads Sub Print()
            MyBase.Print()
        End Sub
    End Class

End Module

, , , , , VB Shadow overloaded.

0

# , , , :

int MethodA(int x){
  return x++;
}

int MethodA(int x, int y){
  return x+y;
}

( #) - , , , . eg.

public class Parent{
   public int MethodX(int x){
         return x++;
   }
}

public class Child : Parent{
   public new int MethodX(int x){
         return x+2;
   }
}

, , , .

-1

Source: https://habr.com/ru/post/1624120/


All Articles