国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# 判斷字符串為空的幾種辦法

2020-01-24 03:28:18
字體:
供稿:網(wǎng)友

1. 三種常用的字符串判空串方法:
Length法:bool isEmpty = (str.Length == 0);
Empty法:bool isEmpty = (str == String.Empty);
General法:bool isEmpty = (str == "");
2. 深入內(nèi)部機(jī)制:
要探討這三種方法的內(nèi)部機(jī)制,我們得首先看看.NET是怎樣實(shí)現(xiàn)的,也就是要看看.NET的源代碼!然而,我們哪里找這些源代碼呢?我們同樣有三種方法:
Rotor法:一個不錯的選擇就是微軟的Rotor,這是微軟的一個源代碼共享項(xiàng)目。
Mono法:另一個不錯的選擇當(dāng)然就是真正的開源項(xiàng)目Mono啦!
Reflector法:最后一個選擇就是使用反編譯器,不過這種重組的代碼不一定就是原貌,只不過是一種“近似值”,你可以考慮使用Reflector這個反編譯器[1]。
這里我采用Reflector法,我們先來看看一下源代碼[2](片段):
復(fù)制代碼 代碼如下:

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);
進(jìn)一步分析:
首先是Empty法,由于String.Empty是一個靜態(tài)只讀域,只會被創(chuàng)建一次(在靜態(tài)構(gòu)造函數(shù)中)。但當(dāng)我們使用Empty法進(jìn)行判空時,.NET還會依次展開調(diào)用以下的方法,而后兩個方法內(nèi)部還會進(jìn)行對象引用判等!
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法判等的話,情況就“更勝一籌”了!因?yàn)?NET除了要依次展開調(diào)用上面三個方法之外,還得首先創(chuàng)建一個臨時的空字符串實(shí)例,如果你要進(jìn)行大量的比較,這恐怕是想一想就很嚇人了!
而對于Length法,我們就可以繞過上面這些繁瑣的步驟,直接進(jìn)行整數(shù)(字符串長度)判等,我們知道,大多數(shù)情況下,整數(shù)判等都要來得快(我實(shí)在想不出比它更快的了,在32位系統(tǒng)上,System.Int32運(yùn)算最快了)!
另外,我們還可以看到,在EqualsHelper方法里面.NET會先使用Length法來進(jìn)行判等!可惜的是我無法獲得InternalLength方法的代碼。但我在Mono的源代碼里面看到更簡明的實(shí)現(xiàn):
復(fù)制代碼 代碼如下:

class String
...{
    private int length;
    public int Length
    ...{
        get
        ...{
            return length;
        }
    }
    // .
}

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

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

3. 最后總結(jié):
從上面的分析我們可以看到,使用Length法來進(jìn)行字符串判空串是有著很大的性能優(yōu)勢的,尤其在進(jìn)行大量字符串判空時!當(dāng)然首先得判斷字符串實(shí)例是否為空引用!
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 象山县| 中方县| 浠水县| 揭东县| 阿合奇县| 天峨县| 荃湾区| 会东县| 高陵县| 宿迁市| 策勒县| 磐石市| 崇左市| 建宁县| 叙永县| 巨野县| 华坪县| 贵港市| 财经| 屯门区| 綦江县| 关岭| 云龙县| 高雄市| 九江县| 当涂县| 将乐县| 稷山县| 电白县| 宁津县| 宜州市| 上栗县| 电白县| 洛阳市| 屏东市| 铁岭市| 黑水县| 达日县| 宜宾县| 洪江市| 巴林左旗|