> ## 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.

# C# New

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.

```csharp theme={"dark"}
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).

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

```csharp theme={"dark"}
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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
