哪一个更好,Enumerable.Empty< T>或新的[0]? [英] Which is better, Enumerable.Empty<T> or new[0]?

查看:222
本文介绍了哪一个更好,Enumerable.Empty< T>或新的[0]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是相同的:

IEnumerable<string> i;
i = Enumerable.Empty<string>();
i = new string[0];



所以,它用?

So, which to use?

我认为首先传达意图更加清晰,但它是更大的,有点吵,更何况在调试器难看。第二个也是内存和CPU更有效,如果我读反射的权利。

I think the first communicates intent more clearly, but it is bigger and a bit noisier, not to mention ugly in the debugger. The second is also more efficient in memory and CPU, if I'm reading Reflector right.

我倾向于在新型[0 ] ,但想知道大家的想法。

I'm leaning towards the new type[0], but wanted to know what you all think.

推荐答案

可枚举.Empty< T> 缓存空数组的创作,所以在同一阵列将永远被退回,而第二个语句创建每次调用一个新的数组

Enumerable.Empty<T> caches the creation of the empty array, so the same array will be always be returned, while the second statement creates a new array with each call.

我会倾向于 Enumerable.Empty< T> ,因为它显示了程序员的意图更加清晰,而且还因为使用一个明确的数组创建因为内存使用的是一个管理程序过早优化,运行时几乎总是比分配必要的内存的进程反正多。

I would tend to Enumerable.Empty<T>, as it shows the intention of the programmer more clearly, and also because using an explicit array creation because of memory usage is premature optimization in a managed program, as the runtime will almost always allocate more than necessary memory to the process anyway.

这篇关于哪一个更好,Enumerable.Empty&LT; T&GT;或新的[0]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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