1. 三種常用的字符串判空串方法:

Length法:bool isEmpty = (str.Length == 0);
Empty法:bool isEmpty = (str == String.Empty);
General法:bool isEmpty = (str == "");
2. 深入內(nèi)部機制:

要探討這三種方法的內(nèi)部機制,我們得首先看看.NET是怎樣實現(xiàn)的,也就是要看看.NET的源代碼!然而,我們哪里找這些源代碼呢?我們同樣有三種方法:
Rotor法:一個不錯的選擇就是微軟的Rotor,這是微軟的一個源代碼共享項目。
Mono法:另一個不錯的選擇當然就是真正的開源項目Mono啦!
Reflector法:最后一個選擇就是使用反編譯器,不過這種重組的代碼不一定就是原貌,只不過是一種“近似值”,你可以考慮使用Reflector這個反編譯器[1]。
這里我采用Reflector法,我們先來看看一下源代碼[2](片段):

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里面String類的代碼與此沒什么不同,只是沒有EqualsHelper方法,代之以如下的聲明:

public extern bool Equals(String value);

進一步分析:
首先是Empty法,由于String.Empty是一個靜態(tài)只讀域,只會被創(chuàng)建一次(在靜態(tài)構(gòu)造函數(shù)中)。但當我們使用Empty法進行判空時,.NET還會依次展開調(diào)用以下的方法,而后兩個方法內(nèi)部還會進行對象引用判等!

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);

若使用General法判等的話,情況就“更勝一籌”了!因為.NET除了要依次展開調(diào)用上面三個方法之外,還得首先創(chuàng)建一個臨時的空字符串實例,如果你要進行大量的比較,這恐怕是想一想就很嚇人了!
而對于Length法,我們就可以繞過上面這些繁瑣的步驟,直接進行整數(shù)(字符串長度)判等,我們知道,大多數(shù)情況下,整數(shù)判等都要來得快(我實在想不出比它更快的了,在32位系統(tǒng)上,System.Int32運算最快了)!
另外,我們還可以看到,在EqualsHelper方法里面.NET會先使用Length法來進行判等!可惜的是我無法獲得InternalLength方法的代碼。但我在Mono的源代碼里面看到更簡明的實現(xiàn):

class String
{
private int length;

public int Length
{

get
{
return length;
}
}

// .
}

然而使用Length法進行字符串判空串時,有一點要注意的,就是你必須先判斷該字符串實例是否為空引用,否則將會拋出NullReferenceException異常!于是,我們有了一個經(jīng)過改進的Length法:

void Foo(string bar)
{
if ((bar != null) && (bar.Length == 0))
//
}

3. 最后總結(jié):
從上面的分析我們可以看到,使用Length法來進行字符串判空串是有著很大的性能優(yōu)勢的,尤其在進行大量字符串判空時!當然首先得判斷字符串實例是否為空引用!
|