在移植最初使用 Overloads 修饰符的 VB 代码时,如何避免在 C# 中隐藏基类型成员?

How can you avoid hiding a base type member in C# when porting VB code originally using the Overloads modifier?

VB.NET's Shadows keyword has an exact equivalent in C#,我在其他地方发现它的 Overloads 关键字并非如此,至少在继承的上下文中是这样。话虽如此,请使用以下代码:

Public Interface IParent

    Property SomeBool As Boolean

End Interface

Public Interface IChild
    Inherits IParent

    Overloads Property SomeBool As Boolean

End Interface

乍一看,在移植接口 IChild 时,如果没有 Overloads 的某种等效项,似乎没有一种明确的方法可以真正捕获 C# 中的确切含义。如果您尝试这样做:

public interface IChild : IParent
{
    bool SomeBool { get; set; }
}

那么您可能会收到以下效果的警告:

'IChild.IsDefaultValue' hides inherited member 'IParent.IsDefaultValue'. Use the new keyword if hiding was intended.

newOverloads正好相反。没有等效的关键字,你怎么能确定它被正确移植了?

虽然这个问题涵盖 类 以及接口,但根据定义,接口会阻止您通过实现来模拟它。

来自文档:

Shadowing and Overloading. 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 parameter list as the base class member, and you do not supply the Shadows keyword.

来源:https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/overloads

因此,当用于阴影时,new 是等效的。