Partager via


new Modifier (C# Reference) 

When used as a modifier, the new keyword explicitly hides a member inherited from a base class. Hiding an inherited member means that the derived version of the member replaces the base-class version. Hiding members without the use of the new modifier is allowed, but generates a warning. Using new to explicitly hide a member suppresses this warning, and documents the fact that the derived version is intended as a replacement.

To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier. 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 by DerivedC.Invoke. 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:

  • A constant, field, property, or type introduced in a class or struct hides all base class members with the same name.

  • A method introduced in a class or struct hides properties, fields, and types, with the same name, in the base class. It also hides all base class methods with the same signature.

  • An indexer introduced in a class or struct hides all base class indexers with the same signature.

It is an error to use both new and override on the same member, as the two modifiers have mutually exclusive meanings. Using new creates a new member with the same name and causes the original member to become hidden, while override extends the implementation for an inherited member.

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

Example

In this example, a base class, BaseC, and a derived class, DerivedC, use the same field name x, thus hiding the value of the inherited field. 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.

// cs_modifier_new.cs
// The new modifier.
using System;
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

In this example, a nested class hides a class with the same name in the base class. The example demonstrates using the new modifier to eliminate the warning message, as well as accessing the hidden class members by using their fully qualified names.

// cs_modifer_new_nested.cs
// Using the new modifier with nested types.
using System;
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

Comments

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'.

You can also use the new modifier to modify a nested type if the nested type is hiding another type, as demonstrated in the following example.

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 10.2.2 The new modifier

See Also

Reference

C# Keywords
Operator Keywords (C# Reference)
Modifiers (C# Reference)

Concepts

C# Programming Guide

Other Resources

C# Reference