NULL, "", String.Empty differences in C #

Source: Internet
Author: User

(1) Null
The NULL keyword is a literal value that represents a null reference that does not reference any object . NULL is the default value for a reference type variable. Then only the variable of the reference type can be null, if int i=null, then it is not possible, because int is a value type.


(2) "" and String.Empty
Both of these represent empty strings. But "theoretically re-opens up the memory space , and String.Empty points to a place." But the optimizer will optimize!

string. Empty does not allocate storage space, "" allocates a blank length of storage space , so it is generally used as string. Empty, in order to cross the platform later, or with String.Empty.

In C #, in most cases "" and string. Empty can be used interchangeably. Like what:

string s = "";

String s2 = string. Empty;

if (s = = string. Empty) {//} if statement established


(3) Several ways to determine the empty string, according to performance from high to low order is:

S.length = = 0 is better than s = = string. Empty better than S = = ""

Attention:

1.string str1= "" and the difference between string str2=null.

STR1 is an empty string, and an empty string is a special string, except that the value of the string is empty, and there is an accurate point in memory.

String str2=null, when defined, simply defines a reference to a string class, and str2 does not point to any place, and will error if not instantiated before use.

Original path: http://www.cnblogs.com/liuyaozhi/p/5809521.html

NULL, "", String.Empty differences in C #

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.