Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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??
Posted

1 solution

you can check for null as below
C#
if(FaultValues!=null)
{
   //FaultValues is not null
}

if you need to check whether the list having any items
C#
if(FaultValues!=null && FaultValues.Any())
{
   //FaultValues is not null and having items 
}

or
C#
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
C#
if( FaultValues!=null && FaultValues.Count==0)
{
   // zero items 
}


Quote:
I forgot to mention that list is not a string but List _lstTempSeverityImg; an image

if it is an image then why you need List? do as below
C#
Image temp;
//when you need to check for null
if(temp==null)
{
  // image is null
}
 
Share this answer
 
v5
Comments
chandra sekhar 10-Jul-14 0:44am    
I forgot to mention that list is not a string but List<system.windows.controls.image> _lstTempSeverityImg; an image
DamithSL 10-Jul-14 0:48am    
unclear! can you past the code where you create the list?
Sergey Alexandrovich Kryukov 10-Jul-14 0:50am    
I just wanted to say: "My 5, but who knows what OP means by list". The OP's comment only confirms that the whole thing is just gibberish; probably, no answer of this sort could help.
—SA
DamithSL 10-Jul-14 1:02am    
Thanks SA, Yes correct. I have updated my answer and it may not be the exact answer for OP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900