new 修饰符(C# 参考)new modifier (C# Reference)

在用作声明修饰符时,new 关键字可以显式隐藏从基类继承的成员。When used as a declaration modifier, the new keyword explicitly hides a member that is inherited from a base class. 隐藏继承的成员时,该成员的派生版本将替换基类版本。When you hide an inherited member, the derived version of the member replaces the base class version. 虽然可以不使用 new 修饰符来隐藏成员,但将收到编译器警告。Although you can hide members without using the new modifier, you get a compiler warning. 如果使用 new 来显式隐藏成员,将禁止此警告。If you use new to explicitly hide a member, it suppresses this warning.

new 关键字还可用于创建类型的实例或用作泛型类型约束You can also use the new keyword to create an instance of a type or as a generic type constraint.

若要隐藏继承的成员,请使用相同名称在派生类中声明该成员,并使用 new 修饰符对其进行修饰。To hide an inherited member, declare it in the derived class by using the same member name, and modify it with the new keyword. 例如:For example:

public class BaseC
{
    public int x;
    public void Invoke() { }
}
public class DerivedC : BaseC
{
    new public void Invoke() { }
}

在此示例中,使用 BaseC.Invoke 隐藏了 DerivedC.InvokeIn this example, BaseC.Invoke is hidden by DerivedC.Invoke. 字段 x 不受影响,因为未使用类似名称将其隐藏。The field x is not affected because it is not hidden by a similar name.

通过继承隐藏名称采用下列形式之一:Name hiding through inheritance takes one of the following forms:

  • 通常,在类或结构中引入的常数、字段、属性或类型会隐藏与其共享名称的所有基类成员。Generally, a constant, field, property, or type that is introduced in a class or struct hides all base class members that share its name. 有三种特殊情况。There are special cases. 例如,如果将名称为 N 的新字段声明为不可调用的类型,并且基类型将 N 声明为一种方法,则新字段在调用语法中不会隐藏基声明。For example, if you declare a new field with name N to have a type that is not invocable, and a base type declares N to be a method, the new field does not hide the base declaration in invocation syntax. 有关详细信息,请参阅 C# 语言规范中的成员查找部分。For more information, see the Member lookup section of the C# language specification.

  • 类或结构中引入的方法会隐藏基类中共享该名称的属性、字段和类型。A method introduced in a class or struct hides properties, fields, and types that share that name in the base class. 它还会隐藏具有相同签名的所有基类方法。It also hides all base class methods that have the same signature.

  • 类或结构中引入的索引器会隐藏具有相同签名的所有基类索引器。An indexer introduced in a class or struct hides all base class indexers that have the same signature.

对同一成员同时使用 newoverride 是错误的做法,因为这两个修饰符的含义互斥。It is an error to use both new and override on the same member, because the two modifiers have mutually exclusive meanings. new 修饰符会用同样的名称创建一个新成员并使原始成员变为隐藏。The new modifier creates a new member with the same name and causes the original member to become hidden. override 修饰符会扩展继承成员的实现。The override modifier extends the implementation for an inherited member.

在不隐藏继承成员的声明中使用 new 修饰符将会生成警告。Using the new modifier in a declaration that does not hide an inherited member generates a warning.

示例Example

在此示例中,基类 BaseC 和派生类 DerivedC 使用相同的字段名 x,从而隐藏了继承字段的值。In this example, a base class, BaseC, and a derived class, DerivedC, use the same field name x, which hides the value of the inherited field. 此示例演示 new 修饰符的用法。The example demonstrates the use of the new modifier. 另外还演示了如何使用完全限定名访问基类的隐藏成员。It also demonstrates how to access the hidden members of the base class by using their fully qualified names.

public class BaseC
{
    public static int x = 55;
    public static int y = 22;
}

public class DerivedC : BaseC
{
    // Hide field 'x'.
    new public static int x = 100;

    static void Main()
    {
        // Display the new value of x:
        Console.WriteLine(x);

        // Display the hidden value of x:
        Console.WriteLine(BaseC.x);

        // Display the unhidden member y:
        Console.WriteLine(y);
    }
}
/*
Output:
100
55
22
*/

示例Example

在此示例中,嵌套类隐藏了基类中同名的类。In this example, a nested class hides a class that has the same name in the base class. 此示例演示如何使用 new 修饰符来消除警告消息,以及如何使用完全限定名来访问隐藏的类成员。The example demonstrates how to use the new modifier to eliminate the warning message and how to access the hidden class members by using their fully qualified names.

public class BaseC 
{
    public class NestedC 
    {
        public int x = 200;
        public int y;
    }
}

public class DerivedC : BaseC 
{
    // Nested type hiding the base type members.
    new public class NestedC   
    {
        public int x = 100;
        public int y; 
        public int z;
    }

    static void Main() 
    {
        // Creating an object from the overlapping class:
        NestedC c1  = new NestedC();

        // Creating an object from the hidden class:
        BaseC.NestedC c2 = new BaseC.NestedC();

        Console.WriteLine(c1.x);
        Console.WriteLine(c2.x);   
    }
}
/*
Output:
100
200
*/

如果移除 new 修饰符,程序仍将编译和运行,但你会收到以下警告:If you remove the new modifier, the program will still compile and run, but you will get the following warning:

The keyword new is required on 'MyDerivedC.x' because it hides inherited member 'MyBaseC.x'.

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范中的 new 修饰符部分。For more information, see The new modifier section of the C# language specification.

另请参阅See also