C# New

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.

Keywords

Example

Keyword: keywords direct program behavior

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

The result of the new operator and the constructor invocation is an instance of the type. This program also shows the default constructor that all classes have—unless another constructor is specified.

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
Squares: abstract

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

Programming tip

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

Garbage collection visualization

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.

ValueType Examples: Int, DateTimeStruct

Summary

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.

Overview:There are many uses of the new keyword. We described how the new operator indicates memory allocation.


C#: Class: Constructor