How to Create an Empty Dictionary with a Collection Expression in C#
A Dictionary usually starts empty and fills up as the program runs. In C#, [] creates that empty dictionary for you, as long as C# can see which dictionary type you want, from the variable's or the property's type.
Syntax
Dictionary<<KeyType>, <ValueType>> <name> = [];
public Dictionary<<KeyType>, <ValueType>> <Name> { get; set; } = [];
| Part | What it does |
|---|---|
Dictionary<<KeyType>, <ValueType>> | The type, which tells C# what [] must become |
[] | An empty collection expression: here, a new, empty dictionary |
{ get; set; } = []; | The same, as the starting value of a property |
Microsoft's collection expressions reference: "A collection expression is a terse syntax that you can assign to many different collection types." Among the types it lists is "Any type that supports a collection initializer, such as System.Collections.Generic.List<T>." Dictionary supports one.
The Example
The program creates an empty stock list of fruit and their counts, adds apples, and prints the count before and after. A Shop class does the same for a property, so every new shop starts with an empty price list.
Prerequisites
- The .NET 10 SDK or newer, which runs a single
.csfile withdotnet run. This guide uses .NET SDK 10.0.404.
Step 1: Write the Code
Create a file named main.cs in an empty folder:

Dictionary<string, int> stock = [];
Console.WriteLine($"Items: {stock.Count}");
stock.Add("apples", 3);
Console.WriteLine($"Items: {stock.Count}");
Shop shop = new();
Console.WriteLine($"Prices: {shop.Prices.Count}");
class Shop
{
public Dictionary<string, decimal> Prices { get; set; } = [];
}
Step 2: Run It
dotnet run main.cs
Items: 0
Items: 1
Prices: 0

stock started with no entries, and Add worked on it straight away: the [] was a real, empty Dictionary. The new shop's Prices was empty too, rather than null.
The command is the same in zsh, bash and PowerShell.
What Happens with var
With var, C# has no type to turn [] into:
var stock = [];
Console.WriteLine(stock.Count);
/Users/you/demo/main.cs(1,13): error CS9176: There is no target type for the collection expression.
The build failed. Fix the build errors and run again.
Write the type on the left, as in Step 1.
Starting with Entries
[] only works empty here. Putting a pair inside fails:
Dictionary<string, int> stock = [new KeyValuePair<string, int>("apples", 3)];
Console.WriteLine(stock.Count);
/Users/you/demo/main.cs(1,33): error CS9215: Collection expression type 'Dictionary<string, int>' must have an instance or extension method 'Add' that can be called with a single argument.
The build failed. Fix the build errors and run again.
To start with entries, use new() with keys in square brackets instead:
Dictionary<string, int> stock = new() { ["apples"] = 3 };
Console.WriteLine(stock.Count);
1
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
var stock = []; | error CS9176: There is no target type for the collection expression. | Write the type: Dictionary<string, int> stock = []; |
Putting key/value pairs inside [] | error CS9215: ... must have an instance or extension method 'Add' that can be called with a single argument. | Use new() { ["apples"] = 3 } |
| Leaving a dictionary property without a default | It starts as null: warning CS8618, then a NullReferenceException when the program reads its Count | Give it = []; |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
CS9176 on a line with [] | var, or nothing else that says which type to create | Declare the type |
CS9215 on a dictionary | The [] has entries in it | Start with [] and add, or use new() { [key] = value } |
FAQ
Is [] the same as new()?
For an empty dictionary, both gave the same result in a quick check: Dictionary<string, int> fromNew = new(); and Dictionary<string, int> fromBrackets = []; both printed a Count of 0. [] is the same short form you use for empty arrays and lists.
Can I use [] in a method argument?
Yes, when the parameter's type is a dictionary: the parameter gives [] its type, just like the variable in Step 1. PrintCount([]);, calling a method that takes a Dictionary<string, int> and prints its Count, printed 0.
Conclusion
Write Dictionary<TKey, TValue> name = [];, or = []; after a property, for a new, empty dictionary. The type must be written out, so var doesn't work, and for starting entries use new() { [key] = value }.
Additional Resources
- Collection expressions, on Microsoft Learn.
Dictionary<TKey,TValue>class, on Microsoft Learn.
