In C#, new does one of three jobs, depending on where it appears. Most often it creates an object: it allocates the object, calls its constructor and gives you back a reference to it. It also appears in the new() generic constraint, which requires a type argument to have a public parameterless constructor, and as the new modifier, which hides an inherited member.
This site is about C++, so the last section compares C#’s new with C++’s.
new creates an object
When you write new followed by a type and arguments, C# allocates memory for the object, runs the matching constructor and returns the result. For a class, the result is a reference to an object on the managed heap. The garbage collector reclaims the object once nothing refers to it.
using System;
class Person
{
public string Name;
public Person(string name)
{
Name = name;
}
}
class Program
{
static void Main()
{
Person p = new Person("Ada");
Console.WriteLine(p.Name); // Ada
}
}
In C# 9 and later, you can leave the type off the right side when the variable already states it: Person p = new("Ada");. Arrays use the same keyword. new int[3] creates an array of three integers, all set to zero.
For a struct, new also calls a constructor. The difference is that a struct is a value, so assigning it copies the whole value, while a class variable holds a reference to a shared object. Class Constructors in C++ covers the C++ version of constructors.
new() as a generic constraint
In a generic method or class, where T : new() says that the type argument must have a public parameterless constructor. Inside the method, new T() then creates an instance of it:
using System.Collections.Generic;
static T Create<T>() where T : new()
{
return new T();
}
// Usage: List<int> numbers = Create<List<int>>();
The constraint only checks for a parameterless constructor. A caller who passes a type without one gets a compile-time error.
The new modifier hides a member
If a derived class declares a member with the same name as an inherited one, the compiler warns you. Adding new tells the compiler that hiding the inherited member is intentional:
using System;
class Animal
{
public void Speak() => Console.WriteLine("...");
}
class Dog : Animal
{
public new void Speak() => Console.WriteLine("Woof");
}
Hiding is not overriding. If you call Speak through an Animal variable that holds a Dog, the Animal version runs, because the choice is based on the variable’s type. To change behavior through a base reference, use virtual in the base class and override in the derived class.
How C++ new differs
In C++, new also allocates an object and calls its constructor, but it returns a pointer, and you are responsible for releasing the memory with delete. C# has no delete, because the garbage collector handles that:
Person* p = new Person("Ada");
// ... use p ...
delete p;
Heap vs Stack Memory in C++ explains where this memory comes from and why it must be freed. For the C++ rules on which code may use a class’s members, see Access Modifiers in C++.