c# - 是集合为空

标签 c#

var customers = new List<BECustomer>();
customers = GetCustomers();


但是我不明白为什么customers[0] == null
customers.Any()==true
customers.Count == 1
customers==null为假

如何检查空客户?

最佳答案

这是有区别的。

您的列表不为null,因为您使用新的List()实例化了它,然后分配了getCustomers()。也许这返回null。

但是列表中的元素可以为null。例如:

customers[0] = null


然后,将数组中的第一个元素设置为null。

总结一下:

if (customers == null)


检查customerzs变量是否指向null

if (customers[0] == null)


检查数组中的第一个元素是否为null

关于c# - 是集合为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5512257/

相关文章:

c# - 当组件加载 html/css 时 blazor 加载微调器

c# - HttpWebResponse 中永无止境的 Stream

c# - 如何使用 signalr 将 json 对象发送到 .net 服务器

c# - Winform自定义控件为什么是 "Object Reference not set to an instance of an object"?

c# - 是否有 C# 方法可以从引用路径中增加或减少文件夹的数量?

C# 泛型接口(interface)协方差

c# - 软键盘与文本框重叠并使它们无法访问

C# - 通过 Gmail 或其他方式发送电子邮件?

C# - 检索最大的 MySQL 列值,递增 1,并在 INSERT 语句中使用

c# - Visual Studio Test Explorer 按名称分组命名空间