C#:.NET:Constructor

.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda LINQ Parse Path Process Property Regex Replace Sort Split Static StringBuilder Substring Switch Tuple

New instantiates a type. It invokes the type's constructor (with a matching argument signature). After completion, the constructor returns a new instance of the specified type. We can then use the instance.


Example. We use the new operator when instantiating instances of a class. You must combine the new operator with the type name and its constructor arguments to create a new instance of the type. You can use any constructor available.

ClassConstructor

Note: The result of the new operator and the constructor invocation is an instance of the type.

Also: This program shows the default constructor that all classes have—unless another constructor is specified.

Based on: .NET 4.5

C# program that uses new operator

using System;

class Perl
{
    public Perl()
    {
	// Public parameterless constructor.
	Console.WriteLine("New Perl()");
    }
    public Perl(int a, int b, int c)
    {
	// Public parameterful constructor.
	Console.WriteLine("New Perl(a, b, c)");
    }
}

class Program
{
    static void Main()
    {
	// Create a new instance of the Perl type.
	// ... Use the new operator.
	// ... Then reassign to another new object instance.
	Perl perl = new Perl();
	perl = new Perl(1, 2, 3);

	// Another Perl instance.
	Perl perl2 = null;
	perl2 = new Perl();

	// Instantiate the Program class.
	// ... No constructor is declared, so there is a default.
	Program program = new Program();
	Console.WriteLine(program != null);
    }
}

Output

New Perl()
New Perl(a, b, c)
New Perl()
True

This program defines two types: Perl and Program. The Perl class contains two user-defined constructors—these are invoked with the new operator. The Program class introduces the Main method and an implicit default constructor.

Tip: To call the constructors in the Perl type, specify the new operator, and then use the Perl() type with a formal parameter list.

Parameter List

Next, the program instantiates the Program class. This may be confusing. The Program type declaration does not have a constructor in the source text. The C# compiler actually inserts one for you, called the implicit default constructor.

Note: This constructor does nothing except set the memory to its default values. The default constructor is public.

Default Constructor

Usage. New has several usages in the C# language. The most common usage is for instantiating new objects. The new keyword can be used as a modifier to indicate that a method is supposed to hide a method from a derived type.

New Modifier

Also: When declaring generic classes, you can specify that a type must be a reference type with the new() constraint.

Generic Class

Memory allocation. The new operator always results in an allocation. When you use the new operator, the memory is allocated and initialized to its default value. Then the constructor logic is executed and it can change the values from their defaults.

Reference types, such as classes, are always allocated on the managed heap. And types that inherit from System.ValueType, which are considered structs, are typically allocated on the stack memory.

ValueTypeStruct

Summary. New is an operator in the C# language. It instantiates types in a unified way. We saw a user-defined and overloaded constructor, and the default constructor for classes (which has no parameters).