asp. The net DBNull. Value null String. Empty difference is analysed

  • 2020-06-19 10:06:09
  • OfStack

First, "null" is in C# (VB.NET is Nothing), which means when a reference object variable "does not reference" any entity (the classic symptom is that an exception like NullException is thrown if a method of this variable is called).

String. Empty is a static public variable that means that an String variable does not contain any characters (equivalent to ""). However, in terms of performance, it is better to use ES12en.Empty than to declare one "" -- obviously, the former is a static variable of the String class, which will only generate one instance anyway, while the latter will probably generate several.


string str="";
// "" : indicates that the data reference storage area is allocated in the stack, the actual data storage area is created in the heap, and the reference address is assigned to a variable, which is allocated in the heap 1 Three empty storage Spaces of length. 
string str=string.Empty;
//string.Empty:  Indicates that a data reference storage area is allocated in the stack, an area is created in the heap for the actual data storage, and a reference address is assigned to a variable, but the storage space created in the heap is not allocated or the data is not stored. 

DBNull.Value is also a static attribute. It is only used for "null value" comparisons in databases (for example, when reading data using DataReader, or when comparing data in a row or column in DataTable). Because a field in SQL is Null, it simply means that the field has "no value", not "no reference" in C#. So pay attention to these issues:

1) If DataReader is used for ExecuteScalar, you must use null for judgment if you are not sure whether the data will be obtained (because of empty references); If you are sure to read at least one piece of data, but are not sure if the data is empty, use ES30en.Value for numerical determination.

2) To continue 1, if a field is convinced that it has no data, it is equivalent to having no characters, which is equivalent to String.Empty and "", so you can use String.Empty or "" (important conclusion: DbNull.Value = String.Empty ="").

3) In addition, if an assignment is made to DataColumn of type string (for example, null), even so, it is virtually impossible to store an null in DataTable (to correspond to the actual value of SQL), it will be converted to String.Empty or "". The judgment method is the same as the "important conclusion".

In a nutshell:

string str ="";

Here is a blank sheet of paper;

string str = null;

Not even white paper.

string.Empty is equivalent to ""

Generally used for string initialization

Such as:


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

And there will be no error:

string a=string.Empty;
Console.WriteLine(a);

Or for comparison:

if(a=="")
if(a==string.Empty)

The above two sentences have the same effect.

string.Empty does not allocate storage space

"" Allocates 1 empty storage space

So we usually use string.Empty

string.empty is still used for cross-platform in the future

In C#, "" and string.Empty are used interchangeably for the most part. Such as:


string s = "";
string s2 = string.Empty;
if (s == string.Empty) {
//
}

The if statement holds

Several ways of writing an empty string, in order of performance from high to low, are:

s.Length == 0 better than s == string.Empty == ""


Related articles: