C # Some methods for judging null strings

Source: Internet
Author: User
Tags reflector

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. Net'sSource code! However, where can we find these sources?CodeWhat about it? 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 ):

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:

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:

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!

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/samsunge808/archive/2008/09/25/2980534.aspx

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.