Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

new modifier (C# Reference)

  • 2023-04-12
Feedback

In this article

When used as a declaration modifier, thenew 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. This assumes that the base class version of the member is visible, as it would already be hidden if it were marked asprivate or, in some cases,internal. Although you can hidepublic orprotected members without using thenew modifier, you get a compiler warning. If you usenew to explicitly hide a member, it suppresses this warning.

You can also use thenew keyword tocreate an instance of a type or as ageneric type constraint.

To hide an inherited member, declare it in the derived class by using the same member name, and modify it with thenew keyword. For example:

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

In this example,BaseC.Invoke is hidden byDerivedC.Invoke. The fieldx 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. For example, if you declare a new field with nameN to have a type that is not invocable, and a base type declaresN to be a method, the new field does not hide the base declaration in invocation syntax. For more information, see theMember lookup section of theC# 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.

It is an error to use bothnew andoverride on the same member, because the two modifiers have mutually exclusive meanings. Thenew modifier creates a new member with the same name and causes the original member to become hidden. Theoverride modifier extends the implementation for an inherited member.

Using thenew modifier in a declaration that does not hide an inherited member generates a warning.

Examples

In this example, a base class,BaseC, and a derived class,DerivedC, use the same field namex, which hides the value of the inherited field. The example demonstrates the use of thenew 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:1005522*/

In this example, a nested class hides a class that has the same name in the base class. The example demonstrates how to use thenew 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:100200*/

If you remove thenew 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# language specification

For more information, seeThe new modifier section of theC# language specification.

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo