How to determine if the string is an empty string? [C #]
Writen by Allen Lee
0. Geng:
This article is written in the discussion - "FXCOP tells me to check if a string is empty with String.Length. ". In fact, people who have used FXCOP know that it will suggest that you use the string.length attribute to determine if the string is an empty string, but do you understand the reason? Today is a bit free, deliberately write this article, I hope it is a bit help.
1. Three commonly used strings to determine the string method:
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 mechanism of these three methods, we have to first see how .NET is implemented, that is, to see the source code of .NET! However, where do we find these source code? We also have three ways:
Rotor method: A nice choice is Microsoft's Rotor, which is a source code shared project of Microsoft. Mono method: Another nice choice is of course true open source project Mono! Reflector method: The last choice is to use the anti-compiler, but this restructuring code is not necessarily an original, but it is only a "approximate value", you can consider using the reflector this contrast [1]. Here I use the Reflector method, let's take a look at the source code [2] (fragment):
public
SeaD
Class
String: IComparable, Icloneable, Iconvertible, IEnumerable, ICompable
<
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 ((a! = Null) && (b! = Null) {return string.equalshelper (a , B);} Return False;} private static unsafe bool equalshelper (string ao, string bo) {// code here int number = provided = limited = i (Num1! = Bo.Length) {Return false;} // code Here} private extern int interface (); public int length {get {return this.internalLength ();}} // code here} Rotor inside the String class code is nothing different, but there is no equalshelper method, in order to statement:
public
Extern
Bool
Equals (String Value);
further analysis:
The first is the EMPTY method, because String.empty is a static read-only domain, will only be created (in a static constructor). However, when we use the EMPTY method to determine, .NET also expands the following methods in turn, and the last two methods will also conduct object reference judgments!
public
Static
Bool
Operator
==
(
String
a,
String
b);
public
Static
Bool
Equals
String
a,
String
b);
Private
Static
Unsafe
Bool
Equalshelper
String
AO,
String
Bo);
If you use the General French, the situation is "more winning"! Because .NET has to first create a temporary empty string instance, if you want to make a lot of comparison, this is afraid that I think it is very scary! For the Length method, we can bypass these cumbersome steps, directly integrate the integer (string length), we know, in most cases, integers must be coming fast (I really can't think of it Faster, on the 32-bit system, System.Int32 is the fastest operation)! In addition, we can also see that in the equalshelper method. Net will use the Length method to judge! Unfortunately, I can't get the code of the INTERNALLENGTH method. But I saw a simpler implementation in Mono's source code: Class
String
{Private int layth; public int layth {get {return length;}} //.
However, when using the Length method, it is important to note, that is, you must first determine if the string instance is an empty reference, otherwise the NullReferenceException will be thrown! So, we have a improved Length method:
Void
FOO
String
Bar)
{IF (Bar! = Null) && (bar.length == 0) //}
3. Final summary:
From the above analysis we can see that using the LENGTH method to perform character crucifidity strings have a lot of performance advantages, especially when making a large number of strings to determine! Of course, you must first determine if the string instance is empty!
See "Reflector: Get The Secret Inside .NET Assembly" in the introduction of Reflector. This code is referring to .NET Framework from the version number of 2.0.3600.0.