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  ww w .jav  a 2 s. c  o 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