new Operator (C# Reference)

Used to create objects and invoke constructors. For example:

Class1 obj  = new Class1();  

It is also used to create instances of anonymous types:

var query = from cust in customers  
            select new { Name = cust.Name, Address = cust.PrimaryAddress };  

The new operator is also used to invoke the default constructor for value types. For example:

int i = new int();  

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

int i = 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 to declare parameterized constructors on a struct type to set its initial values, but this is only necessary if values other than the default are required.

Both value-type objects such as structs and reference-type objects such as classes are destroyed automatically, but value-type objects are destroyed when their containing context is destroyed, whereas reference-type objects are destroyed by the garbage collector at an unspecified time after the last reference to them is removed. For types that contain resources such as file handles, or network connections, it is desirable to employ deterministic cleanup to ensure that the resources they contain are released as soon as possible. For more information, see using Statement.

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.

struct SampleStruct
{
   public int x;
   public int y;

   public SampleStruct(int x, int y)
   {
      this.x = x;
      this.y = y;
   }
}

class SampleClass
{
   public string name;
   public int id;

   public SampleClass() {}

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

class ProgramClass
{
   static void Main()
   {
      // Create objects using default constructors:
      SampleStruct Location1 = new SampleStruct();
      SampleClass Employee1 = new SampleClass();

      // 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:
      SampleStruct Location2 = new SampleStruct(10, 20);
      SampleClass Employee2 = new SampleClass(1234, "Cristina Potra");

      // 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: Cristina Potra, 1234
*/

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

C# Language Specification

For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.

See Also

C# Reference
C# Programming Guide
C# Keywords
Operator Keywords
new
Anonymous Types