使用Linq对对象进行操作,如何创建< string,string>的空字典.容易地? [英] Using Linq to objects, how to create an empty dictionary of <string, string> easily?
问题描述
要创建一个空序列,请使用以下
To create an empty sequence one uses the following
var empty = Enumerable.Empty<string> ();
是否有这么简单的方法来创建空字典?
Is there an equivalent for creating an empty dictionary as easily as this?
推荐答案
没有等效的方法...
No there is no equivalent...
Enumerable.Empty<T>()
的目的是返回空数组的缓存"实例.因此,您可以避免创建新数组(return new T[0];
)的开销.
The purpose of Enumerable.Empty<T>()
is to return a "cached" instance of an empty array. So you can avoid the overhead of creating a new array (return new T[0];
).
您不能将其转换为类似IDictionary<TKey, TValue>
或Dictionary<TKey, TValue>
的非只读结构,因为返回的实例以后可能会被修改,从而使目的无效.
You cannot translate this to a non-readonly structure like a IDictionary<TKey, TValue>
or Dictionary<TKey, TValue>
since the returned instance might be modified later and would therefore invalidate the purpose...
这篇关于使用Linq对对象进行操作,如何创建< string,string>的空字典.容易地?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!