简体   繁体   English

阴影(VB.NET)和新(C#)之间的区别

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

Simple question from a simple-minded: What are the differences between the Shadows keyword in VB.NET and the New keyword in C#? 简单的问题来自一个简单的问题:VB.NET中的Shadows关键字和C#中的New关键字有什么区别? (regarding method signatures of course). (关于方法签名当然)。

They are identical. 它们完全相同。 Shadows is the VB.NET equivalent for C#'s new keyword . Shadows是C#的new关键字的VB.NET等价物。 They mean the same thing semantically and they compile down to the same IL. 它们在语义上意味着相同的东西,并且它们编译成相同的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. 在某些版本的Visual Studio中(我不确定是否仍然如此),在VB.NET项目中使用Shadows关键字可以隐藏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. 它甚至不清楚是设计还是实施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 . 如果您使用来自C#应用程序的相同VB.NET库,您将看到所有方法,就好像它们是用new声明的一样。

They are not identical. 它们完全相同。

The Shadowing concept does not exist in C # C#中不存在Shadowing概念

Consider a vb.net base class with some overloads: 考虑一个带有一些重载的vb.net基类:

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 . DerivedInstance只有一个版本的SomeMethod, 所有其他基本版本都被遮蔽了

if you compile and reference the assembly in a C# project you can see what happens: 如果你在C#项目中编译和引用程序集,你可以看到会发生什么:

DerivedInstance shadows method DerivedInstance阴影方法

To perform hiding in VB.Net, you still have to use the overloads (or overrides if base method is marked as overridable ) keyword: 要在VB.Net中执行隐藏 ,您仍然必须使用重载 (或者如果基本方法被标记为 覆盖,覆盖 )关键字:

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 DerivedInstance隐藏方法

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 #: 所以,在VB.Net中,如果你在一个与基类相匹配的签名上使用了重载关键字,你就会隐藏那个基本版本的方法,就像在c#中一样:

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

Edit: This is the IL code: 编辑:这是IL代码:

From C#: 来自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: 来自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. 注意:在downvote之前......请....尝试。

它们是相同的,它只是实现相同OOP概念的语言特定关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM