A detailed explanation of dbnull.value,null,string.empty difference in asp.net

Source: Internet
Author: User
Tags comparison


First, "null" is in C # (vb.net is Nothing), representing the state of a Reference object variable "unreferenced" any entity (the typical symptom is if a method that calls this variable throws an exception such as Nullexception).

String.Empty is a static public variable that indicates that a string variable does not contain any characters (equivalent to ""). However, in terms of performance, it is better to use a string.empty than a declaration-obviously, the former is a static variable of the string class, which in any case uses only one instance, which may be generated several times.

String Str= "";

"": means that the data reference storage area is allocated in the stack, the actual data storage area is created in the heap, the reference address is assigned to a variable, and a storage space with an empty length is allocated in the heap.

String str=string. Empty;

String. Empty: Indicates that the data reference storage area is allocated in the stack, and the area in the heap for the actual data store is created and the reference address is assigned to the variable, but the storage space created in the heap is not allocated or stores data.

As for DBNull.Value, it is also a static property. It is used only as a "null value" Comparison of a database (for example, when reading data using DataReader, or when a column of data in a DataTable is compared). Because a field in SQL is null only to show that the fields "have no values", it is not "no reference exists" in C #. So note these questions:

1 If you are using DataReader to perform executescalar, if you are unsure whether you will get the data, you must use NULL to judge (because of the null reference); If you are sure to read at least one piece of data, but are unsure whether the data is empty, You can use DBNull.Value to make numerical judgments.

2 to undertake 1, if a field is convinced that there is no data, equivalent to no character, equivalent to String.Empty and "", so can be used String.Empty or "" to judge (important conclusion: dbnull.value=string.empty= " ")。

3 In addition, if you assign a string of DataColumn (for example, an assignment is null), you cannot actually save a null in the DataTable (in order to correspond to the actual SQL value), which translates to String.Empty or "". Judgment method with "important conclusion".

To put it simply:

String str = "";

Give you a blank sheet of paper;

string str = NULL;

There is not even a white paper.

String. Empty is the equivalent of "

Typically used for initialization of strings

Like what:

String A;

Console.WriteLine (a);//There will be an error, because there is no initialization of a

And there is no 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 storage space with an empty length

So you usually use string. Empty

In order to cross the platform later, or with String.Empty

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

string s = "";

String s2 = string. Empty;

if (s = = string. Empty) {

//

}

If statement set up

Several ways to determine an empty string are in the order of performance from highest to lowest:

S.length = 0 is superior to s = = string. Empty is superior to S = = ""

Related Article

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.