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:
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 = 0Better than S = string. Empty than S = ""
About string. the problem with empty and null is that both of them indicate null strings, and one of them focuses on string str1 = string. the difference between empty and string str2 = NULL. After this definition, str1 is an empty string, and the empty string is a special string, except that the value of this string is null, in the memory, there is an accurate point. String str2 = NULL. After this definition, only a reference to the string class is defined, and str2 does not point to any place, if it is not instantiated before use, an error will be reported. Textbox1.text is a zero-length string "".
String. Empty .""
So, I just converted a bunch of CODe that used "" as an empty string like this:
If (mystring = "") anotherstring = "";
To
If (mystring. Equals (string. Empty) anotherstring = string. empty;
And I'm wondering if I did the right thing, using string. Empty. I hate having quoted strings in CoDe and prefer to stay away from them whenever possible. This generally leads to CODe that is more stronugly typed, and easier to maintain, so using string. Empty intuitively feels better than ".But, I 've actually found a concrete reason to use string. Empty-I did someResearchAnd found that in a test, str. Equals (string. Empty) is faster than comparing ""! Well, okay.ResearchIsn' t the right word, "doing onE Google search and accepting on faith the first post I saw "is slightly more accurate. I even created a little graph of the claims in this post here, that string. empty is faster. i'm too lazy to do the test myself, so if you want to verify this, knock yourself out. I do love making graphs though.
That is to say, to judge whether the string is null or not, the best method is str. Length = 0! # C # column