0Pricing
C# Academy · Lesson

Target-Typed new Expressions

Simplify object creation with target typing.

Target-Typed new Expressions is a free C# Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Target-Typed new

Since C# 9, you can write Type x = new(); and omit the type after new when the target type is known.

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        List<int> numbers = new();
        numbers.Add(1);
        Console.WriteLine(numbers.Count);
    }
}

Removing Redundancy

When the variable already states the type, repeating it after new is noise. The empty new() removes it.

using System;
using System.Text;

class Program {
    static void Main() {
        StringBuilder sb = new();
        sb.Append("Hi");
        Console.WriteLine(sb.ToString());
    }
}

Passing Constructor Arguments

Target-typed new() still accepts constructor arguments inside the parentheses.

using System;

class Point {
    public int X, Y;
    public Point(int x, int y) { X = x; Y = y; }
}
class Program {
    static void Main() {
        Point p = new(3, 4);
        Console.WriteLine(p.X + ", " + p.Y);
    }
}

With Object Initializers

You can combine target-typed new with an object initializer block.

using System;

class User {
    public string Name;
    public int Age;
}
class Program {
    static void Main() {
        User u = new() { Name = "Ann", Age = 30 };
        Console.WriteLine(u.Name + " " + u.Age);
    }
}

In Fields

Target-typed new works for field initializers too, keeping declarations concise.

using System;
using System.Collections.Generic;

class Program {
    static List<string> items = new();
    static void Main() {
        items.Add("a");
        Console.WriteLine(items.Count);
    }
}

In Method Arguments

When a parameter has a known type, you can pass new() directly as the argument.

using System;
using System.Collections.Generic;

class Program {
    static void Print(List<int> list) {
        Console.WriteLine(list.Count);
    }
    static void Main() {
        Print(new());
    }
}

Returning new()

If a method return type is known, you can return new() without repeating the type name.

using System;
using System.Collections.Generic;

class Program {
    static List<int> Make() {
        return new() { 1, 2, 3 };
    }
    static void Main() {
        Console.WriteLine(Make().Count);
    }
}

Contrast with var

var x = new List<int>() states the type on the right; List<int> x = new() states it on the left. Pick whichever reads better.

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var a = new List<int>();
        List<int> b = new();
        Console.WriteLine(a.Count + b.Count);
    }
}

Ambiguity Is an Error

If the compiler cannot determine the target type, new() fails. The target must be clear from context.

using System;

class Program {
    static void Main() {
        object o = new object();
        Console.WriteLine(o.GetType().Name);
    }
}

Readability with Long Types

Target-typed new is especially nice for verbose generic types, avoiding writing the type twice.

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Dictionary<string, List<int>> map = new();
        map["a"] = new() { 1, 2 };
        Console.WriteLine(map["a"].Count);
    }
}

A Modern Style

Target-typed new keeps declarations clean while preserving full static typing and compiler checks.

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Queue<string> q = new();
        q.Enqueue("first");
        Console.WriteLine(q.Dequeue());
    }
}

Quick Check

Test your understanding of target-typed new expressions.

Recap

Target-typed new() lets you omit the type after new when the target type is already known, reducing duplication in declarations, fields, arguments, and returns.

It still accepts constructor arguments and initializers, fails when the type is ambiguous, and complements var as a modern, fully type-safe style.

Frequently asked questions

Is the “Target-Typed new Expressions” lesson free?

Yes — the full text of “Target-Typed new Expressions” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Target-Typed new Expressions”?

Simplify object creation with target typing. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Target-Typed new Expressions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Local Variable Type Inference with var
  2. When to Avoid var
  3. The dynamic Type
  4. Target-Typed new Expressions
← Back to C# Academy