new modifier in C#

旧城冷巷雨未停 提交于 2019-12-29 09:32:51

问题


MSDN says:

When used as a modifier, the new keyword explicitly hides a member inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base-class version. Although you can hide members without the use of the new modifier, the result is a warning. If you use new to explicitly hide a member, it suppresses this warning and documents the fact that the derived version is intended as a replacement.

Example:

class Base
{
 int value;

 virtual bool Foo()
 {
   value++;
 }
}

class Derived : Base
{
 int value;

 override bool Foo()
 {
  value++;
 }

}

Do I have to add new modifier to Derived.value declaration? What changes?


回答1:


Since the value field is private, it's not accessible in the derived class. Thus, the declaration in the derived class does not really hide anything. You shouldn't add new to the declaration. If you do, nothing changes, except the compiler will warn you about using new incorrectly. If the value field was accessible in the derived class (e.g. it was public), then you should have used new to express your intention to hide the base member:

class A {
    public int field;
}
class B : A {
    public int field; // warning. `B.field` hides `A.field`. 
}

Using new will silence that warning (it will have no other effect):

class B : A {
    public new int field; // Dear compiler, shut up please.
}

You can't declare a method as both override and new. They are mutually exclusive.




回答2:


Given your example, Derived.value is not "hiding" Base.value. The default access modifier for C# is Private. If you make Base.value Public, then yes, using the new modifer will remove the warning.




回答3:


The new modifier is mainly used for hiding the non virtual methods. Unlike override modifier, it's used to hide all class members not only methods (i.e. variables and properties).

The main purpose comes when you use it to hide a method instead of using override (by the way, to hide a method, you can't use both override and new. This gives you an error not just a waning).

Using virtual and override modifiers will always be similar to using the new modifier except that when you use virtual and override you can't call the the Base class except from inside the Child class itself




回答4:


No you don't, because in this case the base method is marked as virtual, and hence it is intended that base classes override this method.

The case where you might need to use the new modifier is if the base method is not marked as virtual, for example:

class Base
{
    int value;

    public void Foo()
    {
        value++;
    }
}

class Derived : Base
{
    int value;

    new public void Foo()
    {
        value--;
    }
}

Also, in your above example we have to assume that the base Foo method is marked as public or protected, as otherwise the use of the virtual modifier is kind of pointless anyway as it won't be visible to base / other classes.



来源:https://stackoverflow.com/questions/1922550/new-modifier-in-c-sharp

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