检查列表是否为空 [英] Check whether the list is null or not
问题描述
我有一个像List< string>这样的列表FaultValues和我必须检查FaultValue是否为null我必须做某事。但是当分配时(FaultValues == 0)我得到错误,所以如何检查它是否为空?
I have a list like List<string> FaultValues and i have to check if the FaultValue is null i have to do something.But when is assign (FaultValues==0 ) i am getting error so how to check whether it is null or not??
推荐答案
您可以检查以下空值
you can check for null as below
if(FaultValues!=null)
{
//FaultValues is not null
}
如果你需要检查列表是否有任何物品
if you need to check whether the list having any items
if(FaultValues!=null && FaultValues.Any())
{
//FaultValues is not null and having items
}
或
or
if(FaultValues!=null && FaultValues.Count>0))
{
//FaultValues is not null and having items
}
如果您需要检查零项目,
首先检查空列表然后检查项目计数
if you need to check for zero items,
First check for null list then check for item count
if( FaultValues!=null && FaultValues.Count==0)
{
// zero items
}
我忘了提到列表不是字符串而是List _lstTempSeverityImg;一个图像
I forgot to mention that list is not a string but List _lstTempSeverityImg; an image
如果是图像,那你为什么需要List?请执行以下操作
if it is an image then why you need List? do as below
Image temp;
//when you need to check for null
if(temp==null)
{
// image is null
}
这篇关于检查列表是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!