CSharp - Distinguish between an empty string and a null string

Introduction

Calculate the length of an empty string, we'll get zero; but for a null string, we'll encounter a NullReferenceException.

Demo

using System;

public class MainClass
{
    public static void Main(String[] argv)
    {/*from  w w w  . ja  v  a2 s .  co  m*/
        string emptyString = String.Empty;
        string nullString = null;
        Console.WriteLine("Length of emptyString is {0}", emptyString.Length);//0
                                                                              //Console.WriteLine("Length of nullString is {0}", nullString.Length);  //Exception

        Console.WriteLine(emptyString == "");//True
        Console.WriteLine(nullString == "");//False

        Console.WriteLine(string.IsNullOrEmpty(emptyString));//True
        Console.WriteLine(string.IsNullOrEmpty(nullString));//True 
    }
}

Result