Skip to main content

How to Create an Empty Dictionary with a Collection Expression in C#

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
Dictionary<<KeyType>, <ValueType>> <name> = [];

public Dictionary<<KeyType>, <ValueType>> <Name> { get; set; } = [];
PartWhat 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 .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which creates an empty Dictionary of string and int called stock with brackets, prints its Count, adds apples with 3, prints the Count again, and prints the Count of Prices on a new Shop, whose Prices property defaults to brackets.

main.cs
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​

Terminal
dotnet run main.cs
Output
Items: 0
Items: 1
Prices: 0

A zsh terminal where dotnet run main.cs prints Items 0, Items 1 and 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:

main.cs
var stock = [];
Console.WriteLine(stock.Count);
Output
/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:

main.cs
Dictionary<string, int> stock = [new KeyValuePair<string, int>("apples", 3)];
Console.WriteLine(stock.Count);
Output
/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:

main.cs
Dictionary<string, int> stock = new() { ["apples"] = 3 };
Console.WriteLine(stock.Count);
Output
1

Common Mistakes​

MistakeWhat happensDo 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 defaultIt starts as null: warning CS8618, then a NullReferenceException when the program reads its CountGive it = [];

Troubleshooting​

SymptomLikely causeFix
CS9176 on a line with []var, or nothing else that says which type to createDeclare the type
CS9215 on a dictionaryThe [] has entries in itStart 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​