Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The new keyword

Reference for C# new: creating objects, target-typed new(), object and collection initializers, hiding inherited members and the new() generic constraint.

new has three separate jobs in C#. As an operator it creates an object or array and runs a constructor. As a modifier on a member it says "I am deliberately hiding an inherited member with the same name". As a generic constraint, where T : new() requires that a type argument has a public parameterless constructor.

Most of the time you meet the operator. Since C# 9 the type can be omitted when the compiler already knows it (target-typed new), which removes repetition in field and variable declarations.

C#
var list = new List<int>();
Dictionary<string, int> map = new();

Try it

C# Creating objects, initializers and target-typed new

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# new as a modifier and as a constraint

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
new Type(arguments)                       // constructor call
new Type(arguments) { Prop = value }      // object initializer
new Type { 1, 2, 3 }                      // collection initializer
new Type[length]   new Type[] { ... }     // arrays
new(arguments)                            // target-typed (C# 9)
new { A = 1, B = 2 }                      // anonymous type
modifiers new Type Member ...             // hide an inherited member
where T : new()                           // generic constraint
Form Description
Operator Allocates a reference type on the heap (or initializes a struct value) and calls the chosen constructor. Returns the new instance.
Initializers Run after the constructor. Object initializers assign accessible properties or fields; collection initializers call Add.
Member modifier Suppresses warning CS0108 and documents that the member hides, rather than overrides, the base member.
new() constraint The type argument must be a non-abstract type with a public parameterless constructor; a struct always qualifies.

Errors: new on an abstract class or interface is CS0144; no matching constructor is CS1729; calling new T() on an unconstrained T is CS0304.

Usage notes

  • Hiding is not overriding. A member marked new replaces the base member only when the call goes through the derived type. Through a base-typed variable the base member runs (see the second example). Use virtual and override for polymorphism.
  • Target-typed new needs a known type, so var x = new(); is an error. It works for variables with a declared type, fields, parameters, return values and collection expression elements.
  • Structs. new Point() on a struct produces the default value (all zeroes) or calls your parameterless constructor if you declared one (C# 10). default(Point) never runs a constructor.
  • Arrays created with new int[3] are filled with default values; new string[3] holds three nulls.
  • Initializer order. Field initializers, then the base constructor, then the constructor body, then the object initializer.- Anonymous types are immutable, compiler-generated classes with value-like equality, usable only through var or generics.
  • Performance. Allocating a class instance costs a heap allocation; small short-lived objects are cheap but add garbage-collection pressure in hot loops. Prefer struct, stackalloc or pooling only after measuring.
  • Constraint. new T() inside a generic method compiles to Activator.CreateInstance<T>(), which is slower than a direct constructor call.