String. Empty is a static constant of the string class, and "" represents an empty string.
A string is a special type of reference, and its null value means that no memory is allocated.
You can see string by using Ilspy to decompile the string class. The Equalus method overrides the Equalus () method of object: The reference is compared, the contents of the string are compared, the address is the same, and the value must be the same.
1 //overriding the object method2 [__dynamicallyinvokable, ReliabilityContract (Consistency.willnotcorruptstate, Cer.mayfail)]3 Public Override BOOLEquals (Objectobj)4 {5 if( This==NULL)6 {7 Throw NewNullReferenceException ();8 }9 stringText = obj as string;Ten returnText! =NULL&& ( This= = Obj | | ( This. Length = = text. Length &&string. Equalshelper ( This, Text ))); One } A //overloaded equals - [__dynamicallyinvokable, ReliabilityContract (Consistency.willnotcorruptstate, Cer.mayfail)] - Public BOOLEquals (stringvalue) the { - if( This==NULL) - { - Throw NewNullReferenceException (); + } - returnValue! =NULL&& ( This= = Value | | ( This. Length = = value. Length &&string. Equalshelper ( This, value)); +}
String. Empty and Null string comparison results:
1 Static voidMain (string[] args)2 {3String emptystring ="";4 5 //True6Console.WriteLine (emptystring = =String.Empty);7 8 //True9Console.WriteLine (Emptystring.equals (string. Empty));Ten}
string in C #. The difference between empty and "", NULL