string in C #. The difference between empty and "", NULL

Source: Internet
Author: User

  

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.