The blog has moved to the new site F10Debug.com

Sunday, August 16, 2015

string.IsNullOrEmpty C#

C# string.IsNullOrEmpty Method

String.IsNullOrEmpty method checks for string is null or empty.  If string is null or empty, it returns true else it returns false.

Example:

 Here we are going to check example on string with null value, empty string and normal string having some data.

using System;

class Program
{
    static void Main()
    {
 // 1 - string is Null
 string sFirstName = null;
 if (string.IsNullOrEmpty(sFirstName))
 {
     Console.WriteLine("First Name is Null or empty");
 }
 else
 {
     Console.WriteLine("First Name is having some value");
 }

 // 2. string is empty
 string sFirstName = "";
 if (string.IsNullOrEmpty(sFirstName))
 {
     Console.WriteLine("First Name is Null or empty");
 }
 else
 {
     Console.WriteLine("First Name is having some value");
 }

 // 3. string - having some value
 string sFirstName = "Jatin Nahar";
 if (string.IsNullOrEmpty(sFirstName))
 {
     Console.WriteLine("First Name is Null or empty");
 }
 else
 {
     Console.WriteLine("First Name is having some value");
 }
    }
}

Output as below:

1) First Name is Null or empty
2) First Name is Null or empty
3) First Name is having some value

No comments:

Post a Comment