Difference between Shadows (VB.NET) and New (C#)

我怕爱的太早我们不能终老 提交于 2019-12-01 15:37:09

They are not identical.

The Shadowing concept does not exist in C #

Consider a vb.net base class with some overloads:

Public Class BaseClass
    Public Function SomeMethod() As String
        Return String.Empty
    End Function
    Public Function SomeMethod(SomeParam As String) As String
        Return "Base from String"
    End Function

    Public Function SomeMethod(SomeParam As Integer) As String
        Return "Base from Integer"
    End Function
    Public Function SomeMethod(SomeParamB As Boolean) As String
        Return "Base from Boolean"
    End Function
End Class

And this derived class:

Public Class DerivedClass
    Inherits BaseClass

    Public Shadows Function SomeMethod(SomeParam As String) As String
        Return "Derived from String"
    End Function
End Class

Now consider the implementation:

Dim DerivedInstance = New DerivedClass()

DerivedInstance have just one version of SomeMethod, and all other base versions have been shadowed.

if you compile and reference the assembly in a C# project you can see what happens:

DerivedInstance shadows method

To perform hiding in VB.Net, you still have to use the overloads (or overrides if base method is marked as overridable) keyword:

Public Class DerivedClass
    Inherits BaseClass

    Public Overloads Function SomeMethod(SomeParam As String) As String
        Return "Derived from String"
    End Function
End Class

And this is what happens after compiling:

DerivedInstance hide method

So, in VB.Net, if you use overloads keyword, on a signature that matches one on base class, you're hiding that base version of method, just like you would in c #:

public class DerivedClass : BaseClass
{
    public new string SomeMethod(string someParam)
    {
        return "Derived from String";
    }
}

Edit: This is the IL code:

From C#:

.method public hidebysig specialname rtspecialname instance void .ctor () cil managed 
{
    IL_0000: ldarg.0
    IL_0001: call instance void Shadowing_CS.BaseClass::.ctor()
    IL_0006: ret
}

.method public hidebysig instance string SomeMethod (
        string s
    ) cil managed 
{
    IL_0000: ldstr "Derived from string"
    IL_0005: ret
}

From VB:

.method public specialname rtspecialname instance void .ctor () cil managed 
{
    IL_0000: ldarg.0
    IL_0001: call instance void Shadowing_VB.BaseClass::.ctor()
    IL_0006: ret
}

.method public instance string SomeMethod (
        string s
    ) cil managed 
{
    IL_0000: ldstr "Derived from string"
    IL_0005: ret
}

So.... they are not identical.

Note: Before downvote... please.... just try.

They are identical. Shadows is the VB.NET equivalent for C#'s new keyword. They mean the same thing semantically and they compile down to the same IL.

In some versions of Visual Studio (I'm not sure if this is still the case), using the Shadows keyword in a VB.NET project had the effect of hiding all functions with the same name from Intellisense. But that's not actually a language feature; it's not even clear if it's by design or a bug in the implementation of Intellisense. If you use the same VB.NET library from a C# application, you'll see all of the methods as if they were declared with new.

They are same, its just the language specific keyword to implement the same OOP concept.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!