乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C# 關(guān)于判斷字符串為空的一些方法

       orion360doc 2011-01-19
      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法:一個(gè)不錯(cuò)的選擇就是微軟的Rotor,這是微軟的一個(gè)源代碼共享項(xiàng)目。
      Mono法:另一個(gè)不錯(cuò)的選擇當(dāng)然就是真正的開源項(xiàng)目Mono啦!
      Reflector法:最后一個(gè)選擇就是使用反編譯器,不過這種重組的代碼不一定就是原貌,只不過是一種“近似值”,你可以考慮使用Reflector這個(gè)反編譯器[
      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);

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

      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)用上面三個(gè)方法之外,還得首先創(chuàng)建一個(gè)臨時(shí)的空字符串實(shí)例,如果你要進(jìn)行大量的比較,這恐怕是想一想就很嚇人了!
      而對(duì)于Length法,我們就可以繞過上面這些繁瑣的步驟,直接進(jìn)行整數(shù)(字符串長度)判等,我們知道,大多數(shù)情況下,整數(shù)判等都要來得快(我實(shí)在想不出比它更快的了,在32位系統(tǒng)上,System.Int32運(yùn)算最快了)!
      另外,我們還可以看到,在EqualsHelper方法里面.NET會(huì)先使用Length法來進(jìn)行判等!可惜的是我無法獲得InternalLength方法的代碼。但我在Mono的源代碼里面看到更簡(jiǎn)明的實(shí)現(xiàn):

      class String
      {
          
      private int length;

          
      public int Length
          
      {

              
      get
              
      {
                  
      return length;
             
              }

          }


          
      // .
      }


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

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


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

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多