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.
==================================================================================
1. Explanation of the DBNull:
This class is used to indicate that a known value is not present (typically in a database application).
In a database application, an empty object is a valid value for a field. This class distinguishes between null values (empty objects) and uninitialized values (DBNull.Value instances). For example, a table can contain records that have uninitialized fields. By default, these uninitialized fields have a value of DBNull.
The class can also be used to differentiate VT_NULL variables (associated with empty objects) and VT_EMPTY variables (associated with DBNull.Value instances) in COM Interop.
DBNull never equals any value.
DBNull is a separate class, which means that only one instance of the class exists. The only example of this is DBNull.Value.
Data-intensive applications that access SQL databases must use the System.Data.SqlTypes class, which has intrinsic support for null values.
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.
2. 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.
"Null" means the object reference is invalid in. NET and when you retrieve a null value from the Database, it is a valid value to. NET, and it's represented by System.DBNull.Value
Null is used to determine reference invalidate
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, after the definition, just defines a reference to a string class, STR2 does not point to any place, if not instantiated before use, will be an error.
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.
DBNull in Dotnet is a single type of system.dbnull. It has only one value DBNull.Value. DBNull directly inherits Object, so DBNull is not a string, not an int, or a DateTime ...
But why DBNull can represent strings, numbers, or dates in a database. The reason is that the classes that store the data (DataRow, and so on) are stored in the form of object dotnet the data.
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.
In the return value of the executescalar of IDbCommand (Oledbcommand,sqlcommand ...), the situation can be analyzed like this:
Select 1 Returns an object that is 1
Select NULL to return the DBNull.Value
Select IsNull (null,1) returned the 1
Select top 0 ID from table1 This returns a value of NULL
Select IsNull (id,0) from table1 where 1=0 the value returned is null
The ExecuteScalar rule here is to return the first column, the first row of data. If the first row of the first column is not empty, then the executescalar directly corresponds to the value of the dotnet. If there is a first row, but the first column is empty, then the return is DBNull. If there is no line, then ExecuteScalar returns null
This is the rule. One of the easiest mistakes here is to confuse ExecuteScalar return dbnull with NULL, for example:
String Username=cmd. ExecuteScalar (). ToString ();
Unless you think that CMD executes, there must be at least one row of data, otherwise there will be an error.
or select ID from usertable where username= @name such an SQL statement, if the record is not found, then ExecuteScalar will return null, so never
int Userid=convert.toint32 (cmd. ExecuteScalar ());
Or you would write SQL statements like this: Select IsNull (id,0) from usertable where Username= @name
but int userid=convert.toint32 (CMD). ExecuteScalar ()); Will still go wrong, because when the above statement is not true, no rows are returned.
For IDbDataParameter (Oleddbparameter,sqlparameter.) Value, if NULL, indicates that the parameter is not specified or represents default. If DBNull.Value, represents null in SQL
So, if you're going to call a stored procedure that has parameters @val nvarchar = "AABB",
So cmd. parameters["@val"]. The Value=null delegate uses this default "AABB"
and CMD. parameters["@val"]. The Value=dbnull.value delegate uses null to pass to @val
You can use Convert.isdbnull to determine whether a value is DBNull. Note Convert.isdbnull (NULL) is false.
==================================================================================
Null is the C # keyword, which is a literal value that represents a null reference to 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.
String. Empty is the equivalent of "", but they and Null are two different things.
It is said to be: string. Empty does not allocate storage space
"" Allocates a blank length of storage space so string. Empty is more efficient than "". (High efficiency is right, but whether or not to allocate storage space, is unclear, pending verification)
A foreigner has done a test, the test object has 5 kinds, namely: s = = "" s = = string. Empty s.equals ("") s.equals (string. Empty) S.length = = 0
Use the following test code (click on the plus sign below) to determine who is more efficient ~
The results of the final test are as follows:
[s = = "] empty string, 10315.6250 milliseconds short string, 8307.8125 milliseconds long string, 8564.0625 ms
[s = = string. Empty] null string, 3573.4375 milliseconds short string, 8307.8125 milliseconds long string, 8603.1250 ms
[S.equals ("")] empty string, 9517.1875 milliseconds short string, 7537.5000 milliseconds long string, 7576.5625 ms
[S.equals (String. Empty)] Null string, 9540.6250 milliseconds short string, 7515.6250 milliseconds long string, 7607.8125 ms
[s.length = 0] empty string, 443.7500 milliseconds short string,
Null is the C # keyword, which is a literal value that represents a null reference to 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.
String. Empty is the equivalent of "", but they and Null are two different things.
It is said to be: string. Empty does not allocate storage space
"" Allocates a blank length of storage space so string. Empty is more efficient than "". (High efficiency is right, but whether or not to allocate storage space, is unclear, pending verification)
A foreigner has done a test, the test object has 5 kinds, namely: s = = "" s = = string. Empty s.equals ("") s.equals (string. Empty) S.length = = 0
Use the following test code (click on the plus sign below) to determine who is more efficient ~
The results of the final test are as follows:
[s = = "] empty string, 10315.6250 milliseconds short string, 8307.8125 milliseconds long string, 8564.0625 ms
[s = = string. Empty] null string, 3573.4375 milliseconds short string, 8307.8125 milliseconds long string, 8603.1250 ms
[S.equals ("")] empty string, 9517.1875 milliseconds short string, 7537.5000 milliseconds long string, 7576.5625 ms
[S.equals (String. Empty)] Null string, 9540.6250 milliseconds short string, 7515.6250 milliseconds long string, 7607.8125 ms
[s.length = 0] empty string, 443.7500 milliseconds short string, 443.7500 milliseconds long string, 445.3125 ms
It is obvious that the length property of the string is the fastest.
Get the following conclusions:
Use S. Equals ("Stringtocompare") to determine whether a non-empty string is equal, using s.length = = To determine whether it is an empty string (note that this cannot be used to determine the case of a null string, otherwise "the object reference is not set to an instance of the object" error).
Determines whether a string is empty (contains null) in 2.0 with String.IsNullOrEmpty (str); 443.7500 milliseconds long string, 445.3125 ms
It is obvious that the length property of the string is the fastest.
Get the following conclusions:
Use S. Equals ("Stringtocompare") to determine whether a non-empty string is equal, using s.length = = To determine whether it is an empty string (note that this cannot be used to determine the case of a null string, otherwise "the object reference is not set to an instance of the object" error).
In 2.0, determine whether the string is empty (including null) with String.IsNullOrEmpty (str);
Http://www.cnblogs.com/kingge/archive/2011/06/25/2090225.html