If you have written C# collection initializers, you may have seen newer code like int[] ids = [4, 8, 15];. The square brackets are a C# language feature called collection expressions, introduced in C# 12. They make many initializations shorter, but they are target-typed: the type on the left or the method parameter helps determine what collection to create.
That distinction matters. The same expression does not have one standalone collection type, and not every collection can be initialized this way. Here is where the syntax improves clarity, what the spread element does, and when the older form is easier to understand.
Summary
- Collection expressions use
[...]and require a target type that supports the conversion. - C# 12 shipped with .NET 8; the language feature is selected by the compiler/project language version, not by a special runtime API.
..spreads elements from an existing collection into a new target collection.- Use the syntax for straightforward values; keep constructors or builders when they express important setup or semantics.
A basic example
int[] retryDelays = [1, 2, 5];
List<string> regions = ["ap-southeast-1", "eu-west-1"];
IReadOnlyList<double> temperatures = [28.5, 29.0, 27.8];
The compiler sees the target type and converts the expression to a compatible collection. The collection expression itself has no natural type on its own. That is why this is clear:
int[] values = [1, 2, 3];
But assigning it to var is not the equivalent shortcut:
// var values = [1, 2, 3]; // No target type for the collection expression.
Use an explicit array or list type when the target is not otherwise apparent. That small annotation tells readers what object the code intends to create.
Combine data with the spread element
The .. spread element inserts the elements from another collection into the new expression:
string[] defaults = ["en", "ms"];
string[] enabledLanguages = [.. defaults, "ja"];
Here the result is a new array containing en, ms, and ja. The expression copies elements into the result; it does not make defaults and enabledLanguages share a mutable array. If an item is a reference type, the reference is copied, not a deep clone of the object.
Spreading can replace a few lines of list construction when you already know the parts:
List<int> standardPorts = [80, 443];
List<int> testPorts = [.. standardPorts, 8080];
For a large or lazily generated sequence, do not assume collection-expression syntax makes the operation free. It still has to produce a collection, and the compiler may need to materialize values. Use a streaming API such as IEnumerable<T> when you want deferred processing rather than a new in-memory collection.
Use a collection expression at a method call
If the receiving parameter supplies the target type, you can pass the expression directly:
static void PrintNames(IReadOnlyList<string> names)
{
foreach (string name in names)
Console.WriteLine(name);
}
PrintNames(["Aisha", "Daniel"]);
This is concise for a short, local set of values. If the values need to be reused, named, or debugged separately, define a variable instead. The method signature is what makes the argument’s intended collection type legible.
What about empty collections?
An empty expression can be useful when a target type is known:
List<string> warnings = [];
For a method accepting a collection interface, the target parameter type may also provide the needed context. If the compiler cannot infer an intended collection type, provide one explicitly. This is a language conversion, so check the target type’s supported collection-expression conversion rather than assuming any type with Add will work.
Prerequisites and project setup
Collection expressions are available starting with C# 12. A common setup is the .NET 8 SDK and a project targeting net8.0; the SDK includes a compatible compiler and the default language version follows the target framework. Existing projects can explicitly choose a language version in the project file, but avoid setting a newer language version than the compiler and target can support.
For example:
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
This is a C# syntax feature, not a .NET 8 collection class. It is also not an IDE feature: Visual Studio can provide editing support, but the compiler decides whether the syntax is valid. If a project reports a syntax error, inspect the SDK/compiler and LangVersion before changing the code.
When the older initializer is clearer
Keep an explicit constructor when it communicates something important:
var queue = new Queue<string>();
var set = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"admin",
"editor"
};
The constructor carries a comparer in this example. Replacing it with a collection expression would hide that setup or fail to express the same intent. Collection expressions are excellent for initializing a supported collection from values; they are not a general replacement for constructors, builders, or APIs that configure behavior.
Mistakes to avoid
- Expecting
varto infer the type. Give the expression a target type. - Assuming all collection classes are supported. Check conversions or use the constructor.
- Thinking spread means shared storage. The destination is a new collection shape and elements are inserted into it.
- Using it for long, complex construction. A builder or named intermediate variables may explain the steps better.
- Blaming Visual Studio first. Confirm the project SDK and compiler language version.
If you are choosing among C# value types, see our record struct guide. For collections processed in batches, compare this initialization feature with LINQ Chunk.
My Take
I like collection expressions most when they remove ceremony without concealing the collection type. The square brackets make small values easy to scan; the target type still deserves to be visible somewhere nearby. For more complex setup, clarity beats brevity.
Conclusion
C# 12 collection expressions are a useful syntax for creating arrays, lists, and other supported collection targets. Let the destination type make intent clear, use spread for modest composition, and keep constructors when they carry meaningful behavior. Check the project compiler version if the syntax is not recognized.