Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The new operator creates a new instance of a type. It allocates memory for the specified object, invokes the appropriate constructor with the provided arguments, and returns a reference to the allocated memory (for reference types) or the initialized instance (for value types). If there is insufficient memory available to satisfy the allocation request, the new operator throws an OutOfMemoryException.

Memory Allocation Mechanics

The behavior of the new operator depends strictly on the type system category of the target being instantiated:
  • Reference Types (class, record class, delegate, array): The operator allocates memory on the managed heap, zeroes out the allocated memory, executes the constructor, and returns a reference to the object.
  • Value Types (struct, record struct, enum): The operator does not allocate heap memory. Instead, it invokes the constructor to initialize the struct’s fields directly at its declaration site (the execution stack for local variables, or inline within an enclosing heap object for fields).

Syntax and Variations

Standard Instantiation Explicitly declares the type being instantiated.
StringBuilder builder = new StringBuilder();
Target-Typed new Expression (C# 9.0+) Omits the type name when the compiler can infer it from the context (e.g., variable declaration, field assignment, or method argument).
StringBuilder builder = new();
List<int> numbers = new(10);
Object and Collection Initializers Combines instantiation with member initialization in a single statement. The compiler generates code that calls the constructor followed by property/field assignments.
Person p = new Person { Name = "Alice", Age = 30 };
List<int> list = new List<int> { 1, 2, 3 };
Array Instantiation Allocates a contiguous block of memory for a specified number of elements. Elements are initialized to their default values unless an array initializer is provided.
int[] array1 = new int[5];
int[] array2 = new int[] { 1, 2, 3 };
int[] array3 = new[] { 1, 2, 3 }; // Implicitly typed array
Anonymous Types Creates an instance of a compiler-generated, immutable reference type based on the provided property names and values.
var anonymousData = new { Id = 1, Status = "Active" };

Lifecycle and Deallocation

Unlike languages such as C++, C# does not have a corresponding delete operator. Memory allocated on the managed heap via the new operator is tracked and automatically reclaimed by the .NET Garbage Collector (GC) when the object is no longer reachable by the application roots. Value types are deallocated based on their storage location: local variables are deallocated immediately when their enclosing scope is popped from the stack, whereas value types embedded as fields within a reference type are allocated on the managed heap and are deallocated by the GC when the parent object is collected.

Keyword Disambiguation

The new keyword serves multiple distinct purposes in the C# language. This documentation covers the new operator. It should not be confused with:
  • The new Modifier: Used in a member declaration to explicitly hide a member inherited from a base class.
  • The new() Constraint: Used in generic type declarations to specify that a type argument must have a public, parameterless constructor.
Master C# with Deep Grasping Methodology!Learn More