admin管理员组

文章数量:1122792

C# 12 introduced collection expressions and now you can write code like List<int> l = [1, 2, 3];.

When it comes to using them with dictionaries, it seems to work fine when creating an empty one with Dictionary<string, int> d = [];.

However, when it comes to creating one with initial values, none of these seem to compile:

  • Dictionary<string, int> d = [{ "a", 1 }];
  • Dictionary<string, int> d = [("a", 1)];
  • Dictionary<string, int> d = [new KeyValuePair<string, int>("a", 1)];

Is there a way I can create a dictionary containing initial values using collection expressions?

C# 12 introduced collection expressions and now you can write code like List<int> l = [1, 2, 3];.

When it comes to using them with dictionaries, it seems to work fine when creating an empty one with Dictionary<string, int> d = [];.

However, when it comes to creating one with initial values, none of these seem to compile:

  • Dictionary<string, int> d = [{ "a", 1 }];
  • Dictionary<string, int> d = [("a", 1)];
  • Dictionary<string, int> d = [new KeyValuePair<string, int>("a", 1)];

Is there a way I can create a dictionary containing initial values using collection expressions?

Share Improve this question asked Nov 21, 2024 at 22:46 nalkanalka 2,3212 gold badges18 silver badges33 bronze badges 2
  • Dictionary<TKey,TValue> has always had an initializer syntax (since its birth in 2005). It has picked up another since then. It doesn't need a third one: learn.microsoft.com/en-us/dotnet/csharp/programming-guide/… – Flydog57 Commented Nov 21, 2024 at 23:05
  • @Flydog57 I'm aware about the collection initializer syntax, it's what inspired the first of the three attempts listed in the question. Here I'm talking about C# 12's collection expressions specifically. Otherwise I'd probably just be duplicating Proper way to initialize a C# dictionary with values – nalka Commented Nov 21, 2024 at 23:09
Add a comment  | 

1 Answer 1

Reset to default 2

Dictionary<string, int> d = [new KeyValuePair<string, int>("a", 1)]; yields this error message:

Collection expression type 'Dictionary<string, int>' must have an instance or extension method 'Add' that can be called with a single argument.

It leads to a solution to make it compile: creating the following extension method

public static void Add<TKey, TValue>(this Dictionary<TKey, TValue> source, KeyValuePair<TKey, TValue> added)
{
    source.Add(added.Key, added.Value);
}

Note that this extension method will also enable the usage of spread elements with dictionaries like Dictionary<string, int> d2 = [..d];


Similarly, based on the above error message, one might expect the same kind of extension method using a tuple (so (TKey Key, TValue Value) instead of KeyValuePair<TKey, TValue>) to enable Dictionary<string, int> d = [("a", 1)]; but (because Dictionary<TKey, TValue> implements IEnumerable<KeyValuePair<TKey, TValue>> and not IEnumerable<(TKey, TValue)>, more here) it will instead yield:

CS0029 Cannot implicitly convert type '(string, int)' to 'System.Collections.Generic.KeyValuePair<string, int>'

本文标签: cHow to create a dictionary with initial values using collection expressionsStack Overflow