导入 Linq 命名空间后,可以同时使用 .Count() 和 .Count(例如,在实现 IList 的对象上)。
.Count() 将调用 .Count 属性(如果可用),如下所示:C# Count() 扩展方法性能 https://stackoverflow.com/questions/1651301/c-count-extension-method-performance
那么直接使用 .Count 而不是 .Count() 来获得(小)性能增益是否更好?
Update:这里有趣的性能数据(1-2 个数量级):Linq Count() 比 List.Count 或 Array.Length 更快还是更慢? https://stackoverflow.com/questions/981254/is-the-linq-count-faster-or-slower-than-list-count-or-array-length/985279#985279
我个人的偏好是坚持使用现有的方法和属性(如果它们无需强制转换即可使用)。因此我会用Count
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)