How to tell if a string is empty in C # share three ways to judge the word

Source: Internet
Author: User
Tags reflector

1. Three commonly used string method to sentence empty strings:
Length method: bool IsEmpty = (str. Length = = 0);
Empty method: BOOL IsEmpty = (str = = String.Empty);
General method: BOOL IsEmpty = (str = = "");


2. In-depth internal mechanisms:
To explore the internal mechanisms of these three methods, we have to look first. NET is how to achieve, that is to see. NET Source code! However, where do we find these source codes? We also have three ways to:
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 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 using the reflector this anti-compiler [1].
Here I use the reflector method, let's look at the source code [2] (fragment):
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 ((a = null) && (b! = null))
... {
return string. Equalshelper (A, b);
}
return false;
}
private static unsafe bool Equalshelper (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
}
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, because String.Empty is a static read-only domain and is created only 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 = = (String A, string b);
public static bool Equals (string A, string b);
private static unsafe bool Equalshelper (string ao, string bo);
If you use General law, then the situation is "better"! Because. NET except to expand the call to the above three methods, you have to first create a temporary empty string instance, if you want to make a lot of comparisons, this is afraid to think it is very scary!
And for the length method, we can bypass these tedious steps, direct integer (string length) and so on, we know that in most cases, the whole sentence will come fast (I can not think of it faster than it, on 32-bit system, the System.Int32 operation is the fastest)!
In addition, we can see that in the Equalshelper method, the. NET will first use the length method to be sentenced and so on! Unfortunately, I can't get the code for the Internallength method. But I see a more concise implementation in Mono's source code:
Class String
... {
private int length;
public int Length
... {
Get
... {
return length;
}
}
// .
}
However, one thing to note when using the length method for string sentencing is that you must first determine if the string instance is a null reference, or the NullReferenceException exception will be thrown! So, we have an improved length method:
void Foo (String bar)
... {
if (bar! = null) && (bar. Length = = 0))
//
}


3. Final Summary:
From the above analysis, we can see that the use of the length method to string empty strings is a great performance advantage, especially in the large number of string to empty! Of course, first of all you have to determine if the string instance is a null reference!

How to tell if a string is empty in C # share three ways to judge the word

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.