You may also want to do something like so as if Trim is what your after, you
only need to do it once using this method, then test it. Otherwise you
probably need to trim again after the test.
if ( s == null || ((string)(s = s.Trim())).Length == 0 )
{
Console.WriteLine("Null or empty.");
return;
}
--
William Stacey, MVP
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
VMI <vo******@yahoo.com> wrote: How can I validate a null string or an empty string in the same IF
statement?
If I use this:
if (myString.Trim().Length <= 0 || myString == null)
{
//do stuff
}
I'll get a run-time error because I can't trim (or get the length) of a
Null value. But I don't want to have two IFs (one that validates null and
one that validates an empty string). And I don't want to convert the null
value into a something that can be validated.
Do it the other way round:
if (myString==null || myString.Trim().Length==0)
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too