Target-typed new lets C# infer the constructed type from the variable type.

target-typed new Target-typed `new` omits the type name when the target type is clear.

Target-Typed New

start
TargetTypedNew.cs
Replay: real traced execution (multi-file project)
using System;

record Counter(int Start)
{
    public int Next()
    {
        return Start + 1;
    }

    public override string ToString()
    {
        return Start.ToString();
    }
}

class Program
{
    static void Main()
    {
        int start = 5;
        Counter counter = new(start);
        int next = counter.Next();

        Console.WriteLine($"start={start}");
        Console.WriteLine($"next={next}");
    }
}
using System;

record Counter(int Start)
{
    public int Next()
    {
        return Start + 1;
    }

    public override string ToString()
    {
        return Start.ToString();
    }
}

class Program
{
    static void Main()
    {
        int start = 1;
        Counter counter = new(start);
        int next = counter.Next();

        Console.WriteLine($"start={start}");
        Console.WriteLine($"next={next}");
    }
}
using System;

record Counter(int Start)
{
    public int Next()
    {
        return Start + 1;
    }

    public override string ToString()
    {
        return Start.ToString();
    }
}

class Program
{
    static void Main()
    {
        int start = 9;
        Counter counter = new(start);
        int next = counter.Next();

        Console.WriteLine($"start={start}");
        Console.WriteLine($"next={next}");
    }
}
  1. start ← 5, counter ← 5

    17{18    static void Main()19    {20        int start→ 5 = 5; //@start=1, 921        Counter counter→ 5 = new(start5);22        int next = counter5.Next();
  2. next ← 6

    21    Counter counter = new(start);22    int next→ 6 = counter5.Next();2324    Console.WriteLine($"start={start5}");25    Console.WriteLine($"next={next6}");26}
    outputstart=5
    next=6
  1. start ← 1, counter ← 1

    17{18    static void Main()19    {20        int start→ 1 = 1;21        Counter counter→ 1 = new(start1);22        int next = counter1.Next();
  2. next ← 2

    21    Counter counter = new(start);22    int next→ 2 = counter1.Next();2324    Console.WriteLine($"start={start1}");25    Console.WriteLine($"next={next2}");26}
    outputstart=1
    next=2
  1. start ← 9, counter ← 9

    17{18    static void Main()19    {20        int start→ 9 = 9;21        Counter counter→ 9 = new(start9);22        int next = counter9.Next();
  2. next ← 10

    21    Counter counter = new(start);22    int next→ 10 = counter9.Next();2324    Console.WriteLine($"start={start9}");25    Console.WriteLine($"next={next10}");26}
    outputstart=9
    next=10