C#: 方法的預設參數

來源:互聯網
上載者:User
大家都知道在C++中,我們可以為方法的參數指定一個預設值,像這樣:

void foo(int i = 100);

當我們以這種形式調用方法的時候: foo(); 實際上參數i被賦於了預設值,所以相當於調用了foo(100);
然而在C#中是不支援參數的預設值的,那麼如果我們要用到類似的功能應該怎麼實現呢?考慮下面這個例子:

class Buffer
{
      public Buffer(int bufferSize = 100) //Compile Error
     {
            buf = new int [bufferSize];
      }
      private int[] buf;
}

首先當然要為Buffer提供一個無參的建構函式重載:
class Buffer
{
 
      public Buffer(int bufferSize)
     {
            buf = new int[bufferSize];
     }
      public Buffer():this(100)
     {
      }
      private int[] buf;
}   

但這個方法有一個問題就是我們把Buffer的預設大小hard-coding到了代碼裡,這有兩個弊端,一是損害了代碼的可讀性,二是用以上方法,如果Buffer有多個重載的建構函式都用到bufferSize的預設值,一旦你要修改預設值的大小,不得不同時修改多處程式,一旦漏掉了其中的一個,說不定就麻煩大了。

所以,正確的方法是為bufferSize提供一個const的預設值:
class Buffer
{
      private const int defaultBufferSize = 100;
      public Buffer(int bufferSize)
     {
            buf = new int[bufferSize];
      }
      public Buffer():this(defaultBufferSize)
     {
     }
      private int[] buf;
}

觀察編譯器為public Buffer()產生的il代碼

.method public hidebysig specialname rtspecialname
        instance void  .ctor() cil managed
{
  // Code size       20 (0x14)
  .maxstack  2
  IL_0000:  ldarg.0
  IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
  IL_0006:  ldarg.0
  IL_0007:  ldc.i4.s   100 //100即為defaultBufferSize的值
  IL_0009:  newarr     [mscorlib]System.Int32
  IL_000e:  stfld      int32[] Buffer::buf
  IL_0013:  ret
} // end of method Buffer::.ctor

defaultBufferSize的值在相應的調用處被替換成了字面常量(這其實也就是const成員的特性),所以使用defaultBufferSize不會影響public Buffer()的執行效率。而由於const成員隱含了static的特性,所以一個Buffer類只有一個defaultBufferSize的變數,效能的影響也是很小的。

我們可以看到.net 類庫中的許多類都使用了這種方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.