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.Invoke 隱藏。In 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