First, "null" is in C # (VB. NET is Nothing), indicating the status of a referenced object variable "not referenced" Any entity (typical symptom is that if a method of this variable is called, an NullException exception will be thrown ).
String. Empty is a static public variable, indicating that a String variable does not contain any character (equivalent ""). However, in terms of performance, use String. empty is better than declaring a "" -- obviously, the former is a static variable of the String class, and only one instance is generated no matter how it is used, the latter may have to be generated several times.
Copy codeThe Code is as follows:
String str = "";
// "": Indicates that the data reference storage area is allocated to the stack, the actual data storage area is created in the stack, and the reference address is assigned to the variable, an empty storage space is allocated to the heap.
String str = string. Empty;
// String. empty: indicates that the data reference storage area is allocated to the stack, and the region used for actual data storage is created in the stack, and the reference address is assigned to the variable, however, the bucket created in the heap is not allocated or data is not stored.
DBNull. Value is also a static attribute. It is used only for the "null value" Comparison of databases (for example, when reading data using DataReader, or when comparing data in a column of a row in the able. In SQL, if a field is Null, it indicates that the field "has no value" rather than "no reference" in C ". Therefore, pay attention to the following issues:
1) if you use DataReader to execute ExecuteScalar, if you are not sure whether the data will be obtained, you must use null for determination (because of null reference); if you are sure to read at least one piece of data, however, you are not sure whether the data is null. You can use DBNull. value.
2) Undertake 1. If a field is sure that there is no data, it is equivalent to no character, and it is equivalent to String. empty and "", so you can use String. empty or "" (important conclusion: DbNull. value = String. empty = "").
3) In addition, If you assign values to a string-type DataColumn (for example, if the value is null), in fact, a null value cannot be saved in the DataTable (to correspond to the actual SQL value ), will be converted to String. empty or "". The judgment method is the same as the "important conclusion ".
Simply put:
String str = "";
Give you a blank sheet of paper;
String str = null;
No white paper.
String. Empty is equivalent ""
It is generally used for string initialization.
For example:
Copy codeThe Code is as follows:
String;
Console. WriteLine (a); // an error is reported because a is not initialized.
No error is reported below:
Copy codeThe Code is as follows:
String a = string. Empty;
Console. WriteLine ();
Or for comparison:
Copy codeThe Code is as follows:
If (a = "")
If (a = string. Empty)
The above two statements have the same effect.
String. Empty does not allocate storage space
"" Allocate a bucket with an empty Length
Therefore, we generally use string. Empty
For the sake of future cross-platform use string. empty
In C #, "" And string. Empty Can be used interchangeably. For example:
Copy codeThe Code is as follows:
String s = "";
String s2 = string. Empty;
If (s = string. Empty ){
//
}
If statement is true
Several methods for determining as a Null String, in the order of performance from high to low:
S. Length = 0 is better than s = string. Empty is better than s = ""