// Pass by value (in, default), reference (in/out), and reference (out)
void TestFunc(int x, ref int y, out int z) {
  x++;  
  y++;
  z = 5;
}

int a = 1, b = 1, c;  // c doesn't need initializing
TestFunc(a, ref b, out c);
Console.WriteLine("{0} {1} {2}", a, b, c);  // 1 2 5

// Accept variable number of arguments
int Sum(params int[] nums) {
  int sum = 0;
  foreach (int i in nums)
    sum += i;
  return sum;
}

int total = Sum(4, 3, 2, 1);   // returns 10

/* C# 4.0 supports optional parameters. Previous versions required function overloading. */  
void SayHello(string name, string prefix = "") {
  Console.WriteLine("Greetings, " + prefix + " " + name);

SayHello("Strangelove", "Dr.");
SayHello("Mom");

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 createps 的頭像
    createps

    遊戲人生 人生遊戲

    createps 發表在 痞客邦 留言(0) 人氣()