Length method:bool0);
Empty method:bool isEmpty = (str = = String.Empty);
General law:bool"");
2, in-depth internal mechanism: to delve into its internal mechanism, need to review. NET source code, also has three kinds of methods for reference: Rotor method: A good choice is Microsoft's rotor, this is Microsoft's one source code to share the project. Mono method: Another good choice of course is the real open source project Mono! Reflector method: The last option is to use the anti-compiler, but this reorganization of the code is not necessarily the original, but is an "approximation", you can consider the use of reflector this anti-compiler [1].
The reflector method used in this paper, first look at the source code [2] (fragment):
Public Sealed classString:icomparable, ICloneable, IConvertible, IEnumerable, icomparable<string>... {StaticString () ... {string. Empty ="";//Code here}//Code here Public Static ReadOnly stringEmpty; Public Static BOOL operator==(stringAstringb) ... {return string. Equals (A, b);} Public Static BOOLEquals (stringAstringb) ... {if(A = =b) ... {return true;}if((A! =NULL) && (b! =NULL))... {return string. Equalshelper (A, b);}return false;}Private Static unsafe BOOLEqualshelper (stringAostringbo) ... {//Code hereintNUM1 =AO. Length;if(NUM1! =Bo. Length) ... {return false;}//Code here}Private extern intinternallength (); Public intLength ... {Get... {return This. Internallength ();}}//Code here}
Rotor there is no difference in the code of the string class, but there is no Equalshelper method, replace it with the following declaration:
Public extern BOOL Equals (String value);
Further analysis: The first is the empty method, since String.Empty is a static read-only domain and will only be created once (in a static constructor). But when we use the empty method to make a short sentence,. NET will also expand the call to the following methods, and then the two methods will also be used inside the object reference sentence!
Public Static BOOL operator = = (stringstring b);
Public Static BOOL Equals (stringstring b);
Private Static unsafe BOOL Equalshelper (stringstring bo);
If you use General law, then the situation is "better"! Because. NET in addition to expanding the call to the above three methods, you must first create a temporary empty string instance, not suitable for a large number of comparisons.
For the length method, it is possible to bypass these tedious steps and directly perform integer (string length) sentencing. In most cases, integer sentences are all going to come fast (I can't think of any faster than that, on a 32-bit system, the System.Int32 is the fastest)! In addition, in the Equalshelper method, the. NET will first use the length method to be sentenced and so on! Unfortunately, the code for the Internallength method cannot be obtained. A more concise implementation is seen in the source code of Mono:
class String ... {privateint length; Public int Length ... {get... {return length;}} // .}
However, when using the length method to empty strings, it is important to be aware that the string instance is a null reference, otherwise the NullReferenceException exception will be thrown! The following is the improved length method:
void Foo (string bar) ... {ifnull0))//}
Copy Code
3, Summary: The use of the length method to carry out the string is a lot of performance advantages, especially in a large number of string to empty the sentence! Of course, first of all you have to determine if the string instance is a null reference!
OK, today's content is introduced here, C # in judging whether the string is empty method, certainly more than these, welcome to communicate more, there are good ways to share.
C # Judging the string is empty which ways