0Pricing
C# Academy · Lesson

Collection Expression Syntax

Initialize arrays, lists, and spans uniformly.

Collection Expression Syntax is a free C# Academy lesson on CoddyKit — lesson 1 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.

Collection Expressions (C# 12)

C# 12 introduces collection expressions: a single square-bracket syntax to create arrays, lists, spans, and more. Instead of new int[] { 1, 2, 3 } you simply write [1, 2, 3].

using System;

int[] numbers = [1, 2, 3, 4, 5];
Console.WriteLine(string.Join(",", numbers)); // 1,2,3,4,5

One Syntax, Many Targets

The same [...] literal works whether the target is an array, a List<T>, or a Span<T>. The compiler picks the right construction.

using System;
using System.Collections.Generic;

int[] arr = [1, 2, 3];
List<int> list = [4, 5, 6];
Span<int> span = [7, 8, 9];
Console.WriteLine(arr.Length + " " + list.Count + " " + span.Length);

Empty Collections

An empty collection expression is just []. It is clearer than Array.Empty<T>() or new List<T>() and target-types automatically.

using System;
using System.Collections.Generic;

int[] empty = [];
List<string> names = [];
Console.WriteLine(empty.Length + " " + names.Count); // 0 0

Replacing Verbose Initializers

Collection expressions remove the ceremony of the old initializer syntax. Compare the two forms below.

using System;
using System.Collections.Generic;

// old
List<int> oldWay = new List<int> { 1, 2, 3 };
// new
List<int> newWay = [1, 2, 3];
Console.WriteLine(oldWay.Count == newWay.Count); // True

Nested Collections

You can nest collection expressions to build jagged arrays or lists of lists.

using System;

int[][] grid = [[1, 2], [3, 4], [5, 6]];
Console.WriteLine(grid[1][0]); // 3

Expressions as Elements

Elements can be any expression, including variables, method calls, and arithmetic.

using System;

int x = 10;
int[] values = [x, x * 2, x + 5, Math.Abs(-3)];
Console.WriteLine(string.Join(",", values)); // 10,20,15,3

Multidimensional Note

Collection expressions create jagged arrays (int[][]), not rectangular ones (int[,]). Rectangular arrays still use their own syntax.

using System;

int[][] jagged = [[1, 2, 3], [4, 5]];
Console.WriteLine(jagged[0].Length + " " + jagged[1].Length); // 3 2

Working with Span

Because collection expressions can target Span<T> and ReadOnlySpan<T>, the compiler may use stack allocation, avoiding heap garbage for short-lived data.

using System;

ReadOnlySpan<int> stackData = [1, 2, 3, 4];
int total = 0;
foreach (int n in stackData) total += n;
Console.WriteLine(total); // 10

Passing Collections to Methods

When a parameter type is a collection, you can pass a collection expression directly at the call site.

using System;

static int Sum(int[] xs) {
    int t = 0;
    foreach (int x in xs) t += x;
    return t;
}

Console.WriteLine(Sum([2, 4, 6])); // 12

Readability Wins

Collection expressions shine in data-heavy code, like configuration tables or test fixtures, where the brackets keep the focus on the values.

using System;

string[] days = ["Mon", "Tue", "Wed", "Thu", "Fri"];
Console.WriteLine(days[^1]); // Fri

Returning Collections

A method can return a collection expression, with the return type guiding the construction.

using System;

static int[] Primes() => [2, 3, 5, 7, 11];

Console.WriteLine(string.Join(",", Primes())); // 2,3,5,7,11

Quick Check

Check your understanding of collection expression basics.

Recap

You learned the C# 12 collection expression syntax.

  • [1, 2, 3] replaces verbose initializers.
  • [] is an empty collection.
  • It is target-typed: arrays, lists, and spans all work.
  • Nested brackets build jagged arrays.

Frequently asked questions

Is the “Collection Expression Syntax” lesson free?

Yes — the full text of “Collection Expression Syntax” 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 “Collection Expression Syntax”?

Initialize arrays, lists, and spans uniformly. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Collection Expression Syntax” 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