3

I am working on a condition where I have to validate whether the argument is empty or not. Lets assume that argument is Email. I have to check whether the inwards argument Email is empty or not. I can do it in several way but I am not sure which one to proceed with.

I am thinking to check from following statement:

1.Email = "" to check if email is empty string or not. 2. Email isNot Nothing

I wanna know the difference of these two functionality. If there are more function or argument related to validating empty string, You can write that too.

Thanks.

Rajat Pandit
  • 63
  • 2
  • 9
  • 2
    `Email isNot Nothing` is this vb.net? – TheGeneral Mar 02 '20 at 05:43
  • 1
    or [String.IsNullOrWhiteSpace(String) Method](https://learn.microsoft.com/en-us/dotnet/api/system.string.isnullorwhitespace?view=netframework-4.8) which is sometimes better for user input (where *dirty little fingers* can add spaces) – TheGeneral Mar 02 '20 at 05:45

3 Answers3

5

String is a reference type, which means it can have a null reference

Eg

string myString = null;

It can also be empty, which is to say, there is a reference to it, and it has 0 character length

Eg

string myString = "";
// or
string myString = string.Empty;

And just for completeness, it can also have white space

Eg

string myString = "   ";

You can check for null like so

if(myString == null)

You can check for empty

if(myString == "")

// or

if(myString == string.Empty)

You can check for both, not null and not empty

if(myString != null && myString != string.Empty)

You could use Null conditional Operator with Length to check both is not null and not empty

if(myString?.Length > 0)

Or you can use the built in string methods, to make it a little easier

String.IsNullOrEmpty(String) Method

Indicates whether the specified string is null or an empty string ("").

if(string.IsNullOrEmpty(myString))

String.IsNullOrWhiteSpace(String) Method

Indicates whether a specified string is null, empty, or consists only of white-space characters.

if(string.IsNullOrWhiteSpace(myString))

Note : It's worth noting, that IsNullOrWhiteSpace generally more robust when checking user input

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • 1
    Also note microsoft recommends to use `IsNullOrWhiteSpace` or `IsNullOrEmpty` for superior performance. – bansi Mar 02 '20 at 06:29
  • @bansi thanks for that, do you have any source/link for that so i can add it to the answer? – TheGeneral Mar 02 '20 at 06:30
  • [CA1820: Test for empty strings using string length](https://learn.microsoft.com/en-us/visualstudio/code-quality/ca1820?view=vs-2019) – bansi Mar 02 '20 at 06:38
  • 1
    `myString?.Length >= 0` will be true if your string is not null. To make it false when it's empty make a little fix `myString?.Length > 0` – aepot Mar 02 '20 at 06:52
3

Actually in C# string.Empty is equivalent to "". See String.Empty

Best way to check for Empty or Null strings is:

string.IsNullOrEmpty(Email) or you can use string.IsNullOrWhiteSpace(Email) to additionally check for white spaces.

if(!string.IsNullOrEmpty(Email))
{
    // Good to proceed....
}
Voodoo
  • 1,550
  • 1
  • 9
  • 19
1

You should not use IsNot nothing with reference type variable. Instead, Use string.IsNullOrEmpty(Email) together with String.IsNullOrWhiteSpace(Email) while you need to validate email.

Harsh Shah
  • 188
  • 2
  • 6