0Pricing
C# Academy · Lesson

Target Typing Collections

Let the target type drive collection creation.

Target Typing Collections is a free C# Academy lesson on CoddyKit — lesson 3 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.

What Is Target Typing?

Target typing means the compiler infers what a collection expression should build from the context where it is used, rather than from an explicit type on the right-hand side.

using System;

int[] numbers = [1, 2, 3]; // target type is int[]
Console.WriteLine(numbers.GetType().Name); // Int32[]

The Target Drives the Type

The same literal becomes a different concrete type depending on the variable, parameter, or return type it flows into.

using System;
using System.Collections.Generic;

int[] asArray = [1, 2, 3];
List<int> asList = [1, 2, 3];
Console.WriteLine(asArray is int[]);  // True
Console.WriteLine(asList is List<int>); // True

Target Typing in Method Calls

When a method parameter is a collection type, the argument literal is target-typed to that parameter.

using System;
using System.Collections.Generic;

static void Show(List<int> items) =>
    Console.WriteLine(string.Join(",", items));

Show([10, 20, 30]); // target-typed to List<int>

Target Typing in Returns

A method body can return a bare collection expression; the declared return type supplies the target.

using System;
using System.Collections.Generic;

static List<string> Tags() => ["csharp", "dotnet", "ranges"];

Console.WriteLine(Tags().Count); // 3

Element Type Inference

The element type comes from the target. If the target is double[], integer literals widen to double automatically.

using System;

double[] values = [1, 2, 3]; // ints widen to double
Console.WriteLine(values[0] + 0.5); // 1.5

Interfaces as Targets

Collection expressions can target interfaces like IEnumerable<T>, IReadOnlyList<T>, or ICollection<T>; the compiler chooses a suitable concrete type.

using System;
using System.Collections.Generic;

IReadOnlyList<int> ro = [1, 2, 3];
Console.WriteLine(ro.Count); // 3

Ternary and Target Typing

Both branches of a conditional can be collection expressions, each target-typed to the shared result type.

using System;

bool useDefaults = false;
int[] config = useDefaults ? [0, 0, 0] : [1, 2, 3];
Console.WriteLine(string.Join(",", config)); // 1,2,3

var Is Not a Target

You cannot write var x = [1, 2, 3]; because var provides no target type. Give an explicit type, or use the expression where a type is expected.

using System;

// var x = [1, 2, 3]; // compile error: no target type
int[] x = [1, 2, 3]; // ok
Console.WriteLine(x.Length); // 3

Target Typing with Spreads

Target typing and spreads cooperate: the target decides the final collection type while spreads supply the contents.

using System;
using System.Collections.Generic;

int[] src = [1, 2, 3];
List<int> copy = [..src]; // copies into a List<int>
Console.WriteLine(copy is List<int>); // True

Reducing Noise

Target typing removes repeated type names. Compare the redundant old form with the concise new one.

using System;

// old: type repeated
int[] oldWay = new int[] { 1, 2, 3 };
// new: type only on the left
int[] newWay = [1, 2, 3];
Console.WriteLine(oldWay.Length == newWay.Length); // True

Putting It Together

Here a single helper accepts any read-only collection, and callers pass target-typed literals freely.

using System;
using System.Collections.Generic;

static int Count(IReadOnlyCollection<int> c) => c.Count;

Console.WriteLine(Count([1, 2, 3, 4])); // 4

Quick Check

Test your grasp of target typing.

Recap

You learned how collection expressions are target-typed.

  • The destination type (variable, parameter, return) drives construction.
  • Element types can widen to match the target.
  • Interfaces like IReadOnlyList<T> are valid targets.
  • var cannot be a target.

Frequently asked questions

Is the “Target Typing Collections” lesson free?

Yes — the full text of “Target Typing Collections” 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 Typing Collections”?

Let the target type drive collection creation. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Target Typing Collections” 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. Collection Expression Syntax
  2. The Spread Element
  3. Target Typing Collections
  4. Collection Builders
← Back to C# Academy