How can I check whether a string variable is empty or null in C#?

265,197

Solution 1

if (string.IsNullOrEmpty(myString)) {
   //
}

Solution 2

Since .NET 2.0 you can use:

// Indicates whether the specified string is null or an Empty string.
string.IsNullOrEmpty(string value);

Additionally, since .NET 4.0 there's a new method that goes a bit farther:

// Indicates whether a specified string is null, empty, or consists only of white-space characters.
string.IsNullOrWhiteSpace(string value);

Solution 3

if the variable is a string

bool result = string.IsNullOrEmpty(variableToTest);

if you only have an object which may or may not contain a string then

bool result = string.IsNullOrEmpty(variableToTest as string);

Solution 4

Cheap trick:

Convert.ToString((object)stringVar) == ""

This works because Convert.ToString(object) returns an empty string if object is null. Convert.ToString(string) returns null if string is null.

(Or, if you're using .NET 2.0 you could always using String.IsNullOrEmpty.)

Solution 5

if (string.IsNullOrEmpty(myString)) 
{
  . . .
  . . .
}
Share:
265,197
Samantha J T Star
Author by

Samantha J T Star

I'm in the Philippines so if you send some comment I may not be able to answer if I am sleeping :-) The good thing is I work all day until late each night.

Updated on November 24, 2021

Comments

  • Samantha J T Star
    Samantha J T Star over 2 years

    How can I check whether a C# variable is an empty string "" or null?

    I am looking for the simplest way to do this check. I have a variable that can be equal to "" or null. Is there a single function that can check if it's not "" or null?

  • Adam Houldsworth
    Adam Houldsworth over 12 years
    While technically correct I can categorically say I have never seen this method used.
  • molnarm
    molnarm about 12 years
    I had the same problem and the second one doesn't work correctly. Try this: object x=3; bool result = string.IsNullOrEmpty(x as string); 'x as string' will be null so the result is true despite x having a value other than null or an empty string. I didn't find a short solution, used a double check.
  • jk.
    jk. about 12 years
    @MártonMolnár it would have to contain a string 3 is not a string so this is expected try using "3" instead
  • Shaiju T
    Shaiju T over 8 years
    when i use IsEmpty it says: 'string' does not contain a definition for IsEmpty , can i use IsEmpty in msdn or should i use IsNullOrEmpty ?
  • Andrew Liu
    Andrew Liu over 8 years
    Very simple and useful. I wish PHP could have something like this
  • Milan
    Milan over 8 years
    @Lion Liu: Actually I think PHP has exactly as much to offer. See: php.net/manual/en/function.empty.php
  • Tony Trembath-Drake
    Tony Trembath-Drake over 7 years
    I like IsNullOrWhiteSpace (null,empty, or whitespace...)
  • Stokely
    Stokely almost 7 years
    Are we to assume that this conversion of stringVar to a cast object returns empty string for both null and empty string assigned to the stringVar variable, but converting the same stringVar without the cast returns null and empty string instead? Im just trying to find out all the variations.....
  • chadpeppers
    chadpeppers over 6 years
    @AndrewLiu there is you simple do if (empty($my_var))
  • kayleeFrye_onDeck
    kayleeFrye_onDeck over 5 years
    Just to be super-clear, you have to use the class function for string/String, NOT trying to use the function via the object! Eg, string foo; will not allow you to do foo.IsNullOrEmpty();; you need to use it like string.IsNullOrEmpty(foo); This is kind of annoying when coming from other languages that have built in null/0-length-checks for their string objects.
  • Momoro
    Momoro over 4 years
    stom, your question was unclear. You just said IsEmpty, but that is not a valid variable in the .NET C#