How can I determine whether a string is a null string? [C #]

Source: Internet
Author: User
Tags reflector

How can I determine whether a string is a null string? [C #]

 

Written by Allen Lee

 

0. Origin:

This article is written on the basis of the discussions in attention-"fxcop tells me to check whether a string is empty to use string. length". Anyone who has used fxcop knows that it recommends that you use the string. Length attribute to determine whether the string is a null string, but do you understand why? I'm a little idle today. I wrote this article specially.Article.

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 ( = B)
{
Return True;
}
If ( ! =   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
{< br> Get
{< br> 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 Equalshelper ( 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
{< br> Get
{< br> 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!

    1. For details about reflector, see reflector: Get the secret inside. Net assemblies.
    2. This code decompile the. NET Framework with the version 2.0.3600.0.

 

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.