C# New Class Instance

New keyword (constructor invocation)

Types cannot be used without being instantiated. To instantiate a type, you use the new operator, which invokes the logic specified in the constructor. After completion, the constructor returns a new instance of the specified type—this instance can now store data as your C# program executes.

Keywords

This C# example program uses the new operator to call a class constructor.

Example

Note

This is an example program that demonstrates the use of 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 with the new operator.

Class Examples

The result of the new operator and the constructor invocation is an instance of the type. Also, this program text shows the default constructor that all classes have unless another constructor is specified.

Program that uses new operator [C#]

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
Main method

Description. This program defines two types: the class Perl and the class Program. The Perl class contains two user-defined constructors, and these can be invoked with the new operator. The Program class introduces the Main entry point, and it also introduces an implicit default constructor. In the Main method, the two Perl constructors are called. To call the constructors in the Perl type, specify the new operator, and then use the Perl() type with a formal parameter list.

Using default constructors with new. The program also instantiates the Program class. This may be confusing, as 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. This constructor does nothing except set the memory to its default values. The default constructor is public.

Default Constructor

Usage

Programming tip

New has several usages in the C# language. The most common usage is that shown in this article, 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. Also, when declaring generic classes, you can specify that a type must be a reference type with the new() constraint.

New Modifier

Memory allocation

Garbage collection visualization

The new operator in the C# language always results in some sort of 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 allocated on the managed heap always, while types that inherit from System.ValueType, which are considered structs, are typically allocated on the stack memory.

ValueType Struct Examples

Summary

The C# programming language

We examined the new operator in the C# language and its usage when instantiating instances of types. We saw a user-defined and overloaded constructor, and also the default constructor for classes, which is parameterless. We reviewed other uses of the new token in the language, and also some basics about how the new operator indicates memory allocation.

Constructor Tips
.NET