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

73 Views Asked by At

Whereas VB.NET's Shadows keyword has an exact equivalent in C#, I've found elsewhere that the same is not true for its Overloads keyword, at least in the context of inheritance. That being said, take the following code:

Public Interface IParent

    Property SomeBool As Boolean

End Interface

Public Interface IChild
    Inherits IParent

    Overloads Property SomeBool As Boolean

End Interface

At first glance, when porting the interface IChild, there doesn't seem to be a clear way to truly capture the exact meaning in C# without some sort of equivalent for Overloads. If you try to do this:

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

Then you're liable to get a warning to the following effect:

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

new is the exact opposite of Overloads. Without an equivalent keyword, how can you be sure that this is ported correctly?

While this question covers classes as well as interfaces, interfaces by definition block you from emulating this through implementation.

1

There are 1 best solutions below

2
On BEST ANSWER

From the documentation:

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.

Source: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/overloads

So, when used for shadowing, new would be the equivalent.