> ## 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# With Expression

The `with` expression in C# performs non-destructive mutation by producing a new object instance that is a shallow copy of an existing object, with specified properties or fields modified. While frequently used with immutable data models, the `with` expression does not enforce immutability; it merely leaves the original object's state unaltered during the copy operation. If the original object contains standard `set` properties or mutable fields, the new instance remains fully mutable.

```csharp theme={"dark"}
using System;

public record Point(int X, int Y);

public class Program
{
    public static void Main()
    {
        Point originalInstance = new Point(10, 20);
        
        // Create a shallow copy with the X property modified
        Point newInstance = originalInstance with { X = 50 };

        Console.WriteLine(originalInstance); // Output: Point { X = 10, Y = 20 }
        Console.WriteLine(newInstance);      // Output: Point { X = 50, Y = 20 }
    }
}
```

## Supported Types

The `with` operator is natively supported by the following type declarations:

* `record class` (reference types)
* `record struct` (value types)
* `struct` (all value types)
* Anonymous types

## Technical Mechanics

**Compiler Implementation**
When the `with` expression is evaluated, the compiler's behavior depends strictly on the underlying type:

* **For `record class`:** The compiler invokes a hidden, synthesized virtual method named `<Clone>$`. This method calls a copy constructor (e.g., `protected MyRecord(MyRecord original)`) that duplicates the state of the original record. Developers can customize the shallow copy behavior by manually defining this copy constructor.
* **For `struct` and `record struct`:** No `<Clone>$` method or copy constructor is generated. Instead, the compiler emits a standard struct assignment (a member-wise copy) to a temporary variable, mutates the specified properties and fields on that temporary variable, and then yields the resulting value.
* **For Anonymous Types:** The compiler generates a new instance of the anonymous type, copying the unspecified fields from the original instance and applying the new values from the initializer.

**Initialization and `init` Accessors**
The property assignments defined within the `with` block are executed during the object's initialization phase. This allows the `with` expression to assign values to properties defined with `init`-only setters, bypassing the restriction that typically prevents modification after constructor execution.

**Shallow Copy Semantics**
The `with` operator performs a strictly shallow copy.

* **Value Types:** Nested value types are copied by value.
* **Reference Types:** Nested reference types are copied by reference. The newly created object will hold pointers to the exact same memory addresses for any nested objects, collections, or arrays as the original instance.

## Syntax Rules and Constraints

* **Empty Initializer:** The initializer block can be empty (`originalInstance with { }`). This produces an exact shallow copy of the original object with no modifications.
* **Member Accessibility and Mutability:** You can only modify members that are accessible (e.g., `public` or `internal`). Properties must have an accessible `set` or `init` accessor. Fields must be mutable (i.e., not declared as `readonly` or `const`).
* **Type Preservation:** The compile-time type of a `with` expression is identical to the compile-time type of the operand. Polymorphic cloning is supported for `record class` types; because the synthesized `<Clone>$` method is virtual, if the runtime type derives from the compile-time type, the clone will correctly maintain the derived runtime type.

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