What are the differences and connections among String. Empty, NULL, and "" In asp.net (c ?,
First, let's look at the following code. What do you think are the results?
string str = string.Empty;string str1 = "";string str2 = null;bool t = string.IsNullOrEmpty(str);bool t1 = string.IsNullOrEmpty(str1);bool t2 = string.IsNullOrEmpty(str2);
View Code
The output result is displayed. All are true. Is it depressing? Why is it true. Is there no difference?
The three assignment methods. I must have written all the code. After all, I learned the following online. Share this
On the network: string. Empty is equivalent "",
But here we are talking about "equivalent", not "equal"
It is clear that the above values are all assigned, but if this is the case, an error will be reported. It also verifies that initialization is required before use, that is, the value assignment.
string str1;bool t1 = string.IsNullOrEmpty(str1);Console.WriteLine(str1);
View Code
Since the value assignment is equal. Where is the difference? Is memory allocation?
The most common interview question is: What is the difference between string str = "" and string str = null, which should be familiar to everyone. The answer is: the former allocates space with a Null String, that is, the length is null, but the latter does not allocate space at all. So is the latter cost-effective in terms of efficiency?
String str = string. empty does not allocate memory space either. In the beginning, Empty is equivalent to "", but now Empty does not allocate memory space like null. How can it become a family.
There is another saying on the Network: to use string. Empty for cross-platform platforms in the future. Amount. "" And null are not cross-platform?
The following is an explanation on the network:
String. Empty and Null, both of which indicate Empty strings
String str1 = String. Empty. After this definition, str1 is an Empty string, and the Empty String is a special string,
However, the value of this string is null, Which is accurate to the memory.
String str2 = null. After this definition, a reference to the string class is defined. str2 does not point to any place. If it is not instantiated before use, an error is returned. TextBox1.Text is a zero-length string "".
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 = ""
The best way to judge whether the string is null is s. Length = 0!
Then I found an interesting experiment:
A foreigner tests five types of objects, which are more efficient:
s == ""s == string.emptys.equals(”")s.equals(string.empty)s.length == 0
View Code
The test result is as follows:
[S = ""]
Empty string, 10315.6250 Ms short string, 8307.8125 ms long string, 8564.0625 Ms
[S = string. empty]
Empty string, 3573.4375 Ms short string, 8307.8125 ms long string, 8603.1250 Ms
[S. equals ("")]
Empty string, 9517.1875 Ms short string, 7537.5000 ms long string, 7576.5625 Ms
[S. equals (string. empty)]
Empty string, 9540.6250 Ms short string, 7515.6250 ms long string, 7607.8125 Ms
[S. length = 0]
Empty string, 443.7500 Ms short string, 443.7500 ms long string, 445.3125 Ms
Obviously, the length attribute of a string is the fastest.
The following conclusions are obtained:
Use s. equals ("stringtocompare") to determine whether non-null strings are equal. Use s. length = 0 to determine whether the string is a null string (note that this cannot be used to determine whether the string is null, otherwise the error "the object reference is not set to the instance of the object" will occur ).
Use string. isnullorempty (str) to judge whether the string is null in 2.0 );
The passing experts came in to discuss the differences between the three and the friendly and positive speeches. Are they just as explained above. Thank you!