143

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?

zcoop98
  • 2,590
  • 1
  • 18
  • 31
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • I specifically think you didn't explain this in-depth. You said `to "" or null`, then `not "" or null`, which was throwing me off. – Momoro Nov 06 '19 at 03:42
  • The duplicate closure direction here should be reversed. https://stackoverflow.com/questions/7626375/easier-way-of-writing-null-or-empty is phrased worse, has far fewer views, no better answers, and is younger than this question. – TylerH Jun 28 '23 at 13:52

6 Answers6

293
if (string.IsNullOrEmpty(myString)) {
   //
}
oopbase
  • 11,157
  • 12
  • 40
  • 59
  • 2
    when i use `IsEmpty` it says: `'string' does not contain a definition for IsEmpty` , can i use `IsEmpty` in [msdn](https://msdn.microsoft.com/en-us/library/system.web.webpages.stringextensions.isempty%28v=vs.99%29.aspx) or should i use `IsNullOrEmpty` ? – Shaiju T Nov 03 '15 at 14:49
  • 3
    Very simple and useful. I wish PHP could have something like this – Andrew Liu Dec 17 '15 at 05:23
  • 6
    @Lion Liu: Actually I think PHP has exactly as much to offer. See: http://php.net/manual/en/function.empty.php – Milan Feb 02 '16 at 20:09
  • 5
    I like IsNullOrWhiteSpace (null,empty, or whitespace...) – Tony Trembath-Drake Feb 25 '17 at 13:16
  • 1
    @AndrewLiu there is you simple do if (empty($my_var)) – chadpeppers Dec 18 '17 at 19:10
  • 3
    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. – kayleeFrye_onDeck Mar 05 '19 at 03:14
  • `stom`, your question was unclear. You just said `IsEmpty`, but that is not a valid `variable` in the .NET C# – Momoro Nov 06 '19 at 03:44
64

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);
Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
madd0
  • 9,053
  • 3
  • 35
  • 62
13

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);
jk.
  • 13,817
  • 5
  • 37
  • 50
  • 1
    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. – molnarm Apr 17 '12 at 14:14
  • 1
    @MártonMolnár it would have to contain a string 3 is not a string so this is expected try using "3" instead – jk. Apr 17 '12 at 16:59
3
if (string.IsNullOrEmpty(myString)) 
{
  . . .
  . . .
}
Marek Grzenkowicz
  • 17,024
  • 9
  • 81
  • 111
Asker
  • 725
  • 1
  • 4
  • 14
2

string.IsNullOrEmpty is what you want.

cristobalito
  • 4,192
  • 1
  • 29
  • 41
1

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.)

mfluehr
  • 2,832
  • 2
  • 23
  • 31
yrk
  • 67
  • 3
  • 10
  • 4
    While technically correct I can categorically say I have never seen this method used. – Adam Houldsworth Nov 22 '11 at 09:43
  • 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..... – Stokely Aug 29 '17 at 21:39