1. Three Common empty string determination methods:
Length Method: bool isEmpty = (str. Length = 0 );
Empty method: bool isEmpty = (str = String. Empty );
General method: bool isEmpty = (str = "");
2. in-depth internal mechanism:
To explore the internal mechanisms of these three methods, we need to first look at how. NET is implemented, that is, to look at the source code of. NET! However, where can we find the source code? We also have three methods:
Rotor Method: A good choice is Microsoft's Rotor, which is a source code sharing project of Microsoft.
Mono method: Another good choice, of course, is the real Open-Source Project Mono!
Reflector method: The last choice is to use the anti-compiler. However, the restructured code is not necessarily the original appearance, but an approximate value ", you can consider using the Reflector anti-compiler [1].
Here I use the Reflector method. Let's take a look at the source code [2] (fragment ):
Copy codeThe Code is as follows: public sealed class String: IComparable, ICloneable, IConvertible, IEnumerable, IComparable <string>
...{
Static String ()
...{
String. Empty = "";
// Code here
}
// Code here
Public static readonly string Empty;
Public static bool operator = (string a, string B)
...{
Return string. Equals (a, B );
}
Public static bool Equals (string a, string B)
...{
If (a = B)
...{
Return true;
}
If (! = Null) & (B! = Null ))
...{
Return string. jsonshelper (a, B );
}
Return false;
}
Private static unsafe bool extends shelper (string ao, string bo)
...{
// Code here
Int num1 = ao. Length;
If (num1! = Bo. Length)
...{
Return false;
}
// Code here
}
Private extern int InternalLength ();
Public int Length
...{
Get
...{
Return this. InternalLength ();
}
}
// Code here
}
The code of the String class in the Rotor is no different from this, but there is no javasshelper method. It is replaced with the following declaration:
Public extern bool Equals (String value );
Further analysis:
First, the Empty method. Because String. Empty is a static read-only domain, it will only be created once (in the static constructor ). However, when we use the Empty Method for Empty determination,. NET will call the following methods in sequence, and the next two methods will also be used for Object Reference determination!
Public static bool operator = (string a, string B );
Public static bool Equals (string a, string B );
Private static unsafe bool extends shelper (string ao, string bo );
If the General law is used, the situation will be "better! Because. in addition to calling the above three methods in sequence, you must first create a temporary Null String instance. If you want to compare a large number of values, it may be scary to think about it!
For the Length method, we can bypass the tedious steps above and directly judge the integer (String Length). We know that in most cases, integer determination and so on are faster (I really cannot think of faster than it, in 32-bit systems, System. int32 is the fastest )!
In addition, we can also see that in the javasshelper method,. NET will first use the Length method for determination! Unfortunately, I cannot get the code of the InternalLength method. However, I can see more concise implementation in the Mono source code:Copy codeThe Code is as follows: class String
...{
Private int length;
Public int Length
...{
Get
...{
Return length;
}
}
//.
}
However, when using the Length method to determine whether a string is null or not, you must first determine whether the string instance is null. Otherwise, an NullReferenceException will be thrown! Therefore, we have an improved Length method:Copy codeCode: void Foo (string bar)
...{
If (bar! = Null) & (bar. Length = 0 ))
//
}
3. Summary:
From the above analysis, we can see that using the Length method for string null determination has a great performance advantage, especially when a large number of strings are empty! Of course, you must first determine whether the string instance is a null reference!