我有一个 list 。 list.Count > 0
和 list.Count != 0
有区别吗?或者这些代码有什么性能差异?
if (list.Count > 0)
// do some stuff
if (list.Count != 0)
// do some stuff
note:
list.Count
Can't be less than ziro..
最佳答案
这实际上没有区别,因为列表永远不会少于 0 个项目,但是用于整数比较的 ==
非常快,所以它可能比 >
更快。一个看起来更酷的方法是 list.Any()
。
(这里假设列表是指列表类型或任何内置的 IEnumerable/Collection)
关于c# - list.Count > 0 和 list.Count != 0 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33050573/