使用Linq对对象进行操作,如何创建< string,string>的空字典.容易地? [英] Using Linq to objects, how to create an empty dictionary of <string, string> easily?

查看:88
本文介绍了使用Linq对对象进行操作,如何创建< string,string>的空字典.容易地?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要创建一个空序列,请使用以下

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对对象进行操作,如何创建&lt; string,string&gt;的空字典.容易地?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆