Export (0) Print
Expand All

new

In C#, the new keyword can be used as an operator or as a modifier.

  • new operator   Used to create objects on the heap and invoke constructors.
  • new modifier   Used to hide an inherited member from a base class member.

new Operator

The new operator is used to create objects and invoke constructors, for example:

Class1 MyClass  = new Class1();

It is also used to invoke the default constructor for value types, for example:

int myInt = new int();

In the preceding statement, myInt is initialized to 0, which is the default value for the type int. The statement has the same effect as:

int myInt = 0;

For a complete list of default values, see Default Values Table.

Remember that it is an error to declare a default constructor for a struct, because every value type implicitly has a public default constructor. It is possible though to declare parameterized constructors on a struct type.

Value-type objects such as structs are created on the stack, while reference-type objects such as classes are created on the heap.

The new operator cannot be overloaded.

If the new operator fails to allocate memory, it throws the exception OutOfMemoryException.

Example

In the following example, a struct object and a class object are created and initialized by using the new operator and then assigned values. The default and the assigned values are displayed

// cs_operator_new.cs
// The new operator
using System;
class NewTest 
{
   struct MyStruct 
   {
      public int x;
      public int y;
      public MyStruct (int x, int y) 
      {
         this.x = x;
         this.y = y;
      }
   }

   class MyClass 
   {
      public string name;
      public int id;

      public MyClass () 
      {
      }

      public MyClass (int id, string name) 
      {
         this.id = id;
         this.name = name;
      } 
   }

   public static void Main() 
   {
      // Create objects using default constructors:
      MyStruct Location1 = new MyStruct();
      MyClass Employee1 = new MyClass();

      // Display values:
      Console.WriteLine("Default values:");
      Console.WriteLine("   Struct members: {0}, {1}", 
         Location1.x, Location1.y);
      Console.WriteLine("   Class members: {0}, {1}", 
         Employee1.name, Employee1.id);

      // Create objects using parameterized constructors::
      MyStruct Location2 = new MyStruct(10, 20);
      MyClass Employee2 = new MyClass(1234, "John Martin Smith");

      // Display values:
      Console.WriteLine("Assigned values:");
      Console.WriteLine("   Struct members: {0}, {1}", 
         Location2.x, Location2.y);
      Console.WriteLine("   Class members: {0}, {1}", 
         Employee2.name, Employee2.id);
   }
}

Output

Default values:
   Struct members: 0, 0
   Class members: , 0
Assigned values:
   Struct members: 10, 20
   Class members: John Martin Smith, 1234

Notice in the example that the default value of a string is null. Therefore, it was not displayed.

new Modifier

Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.

Consider the following class:

public class MyBaseC 
{
   public int x;
   public void Invoke() {}
}

Declaring a member with the name Invoke in a derived class will hide the method Invoke in the base class, that is:

public class MyDerivedC : MyBaseC
{
   new public void Invoke() {}
}

However, the field x will not be 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. For more information, see 3.6 Signatures and overloading.
  • 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.

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

For more information on hiding names, see 3.7.1 Name hiding.

For more information on fully qualified names, see 3.8.1 Fully qualified names.

Example

In this example, a base class, MyBaseC, and a derived class, MyDerivedC, 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 the fully qualified names.

// cs_modifier_new.cs
// The new modifier
using System;
public class MyBaseC 
{
   public static int x = 55;
   public static int y = 22;
}

public class MyDerivedC : MyBaseC 
{
   new public static int x = 100;   // Name hiding
   public static void Main() 
   {
      // Display the overlapping value of x:
      Console.WriteLine(x);

      // Access the hidden value of x:
      Console.WriteLine(MyBaseC.x);

      // Display the unhidden member y:
      Console.WriteLine(y);
   }
}

Output

100
55
22

If you remove the new modifier, the program will still compile and run, but you will get the 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.

Example

In this example, a nested class, MyClass, 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 the fully qualified names.

// cs_modifer_new_nested.cs
// Using the new modifier with nested types
using System;
public class MyBaseC 
{
   public class MyClass 
   {
      public int x = 200;
      public int y;
   }
}

public class MyDerivedC : MyBaseC 
{
   new public class MyClass   // nested type hiding the base type members
   {
      public int x = 100;
      public int y; 
      public int z;
   }

   public static void Main() 
   {
      // Creating object from the overlapping class:
      MyClass S1  = new MyClass();

      // Creating object from the hidden class:
      MyBaseC.MyClass S2 = new MyBaseC.MyClass();

      Console.WriteLine(S1.x);
      Console.WriteLine(S2.x);   
   }
}

Output

100
200

See Also

C# Keywords | Operator Keywords | Modifiers

Show:
© 2014 Microsoft