Export (0) Print
Expand All
1 out of 5 rated this helpful - Rate this topic

new Operator (C# Reference)

Used to create objects and invoke constructors. For example:

Class1 o  = new Class1();

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.

Value-type objects such as structs are created on the stack, while reference-type objects such as classes are created on the heap. Both types of objects are destroyed automatically, but objects based on value types are destroyed when they go out of scope, whereas objects based on reference types are destroyed at an unspecified time after the last reference to them is removed. For reference types that consume fixed resources such as large amounts of memory, file handles, or network connections, it is sometimes desirable to employ deterministic finalization to ensure that the object is destroyed as soon as possible. For more information, see using Statement (C# Reference).

The new operator cannot be overloaded.

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

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;
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 MainClass
{
   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, "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 is not displayed.

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

  • 7.5.10 The new operator

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.