The difference between string.empty,null and ""

Source: Internet
Author: User
Tags dotnet

the difference between String.empty,null and ""

String. Empty is equivalent to ""
Typically used for initialization of strings
Like what:
String A;
Console.WriteLine (a);//error here, because there is no initialization of a

And the following will not error:
String a=string. Empty;
Console.WriteLine (a);

or For comparison:
if (a== "")
if (a==string. Empty)
The above two sentences are the same effect.

string. Empty does not allocate storage space
"" Allocates a blank length of storage space
So I usually use 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 is established


String.Empty and NULL, both of which represent an empty string, string str1= String.Empty, so that after the definition, str1 is an empty string, and the empty string is a special string, except that the value of the string is empty. In memory is accurate point , string str2=null, so defined, just define a string class reference, STR2 does not point to any place, if not instantiated before use, will be an error . The value of TextBox1.Text is a zero-length string "".

There are several ways to determine an empty string, according to the order of performance from highest to lowest:
S.length = = 0 is better than s = = string. Empty better than S = = ""

The best way to judge whether a string is empty is s.length==0!

The judgment of NULL in C # is more troublesome, not as simple as in VB6, these various null values of judgment and understanding for unfamiliar people, may be very troublesome, now I in the use of the process of a little experience and shared with you.

(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) DBNULL

DBNULL is a single type in dotnet, and the class can only have a unique instance, DBNULL. Value,dbnull's only function is to represent a string, number, or date in a database, and why it can be expressed because the class that stores the data dotnet (DataRow, etc.) is stored in the form of object. For a DataRow, the value returned by its row[column] is never null, or it is the value of the type of column that is specific. Or it's DBNull. So Row[column]. The ToString () is never written in ToString where NullReferenceException occurs. DBNull implements the IConvertible. However, except that ToString is normal, other toxxx will throw an error that cannot be converted.

(3) "" and String.Empty

Both of these represent empty strings, one of which focuses on the difference between string str1= "" and string str2=null, so that after the definition, str1 is an empty string, and the empty string is a special string, except that the value of the string is empty and there is an exact 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 if it is not instantiated before it is used, it will be held wrong.

(4) Convert.isdbnull ()

Convert.isdbnull () returns an indication of whether the specified object is a DBNull type, which is used to determine whether the object is DBNull. Its return value is true or flase.

Well, said so much, do not know to say that there is no, in fact, these broad sense of "empty value" after understanding, or there is a great difference, or even no relationship at all. is entirely two concepts.

The difference between string.empty,null and ""

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.