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
1 Answer
Reset to default 2Dictionary<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
版权声明:本文标题:c# - How to create a dictionary with initial values using collection expressions? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736306755a1933070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论