2013年10月9日 星期三

[ C# ] Call by Reference

    剛踏入C#的領域裡面,從C/C++這領域跳過來後。以前一直超常使用指標跟參考的我,頓時覺得要用到指標跟參考時還要特別選擇unsafe mode感覺有點雞肋阿。

    難道真的只有一種方法嗎?最近剛好找到關鍵字 ref,一個可以用在方法宣告上把參數變成傳址的方式。突然有點想哭的感覺,這就是熟悉感阿阿阿阿阿!!!


code :

  class Program
  {

    static void Main( string[] args )
    {

      int refValu = 0;

      PrintText( ref refValu );
      PrintText( ref refValu );
      PrintText( ref refValu );

      Console.ReadKey( true );

    }

    static void PrintText( ref int count )
    {

      Console.WriteLine( count );

      count++;

    }

  }

沒有留言:

張貼留言