參數數組(關鍵字 params):
using System;
class MyClass
{
static int Sum(params int[] arr)
{
int count = 0;
foreach(int i in arr) count += i;
return count;
}
static void Main()
{
int n = Sum(1,2,3);
Console.WriteLine(n); //6
n = Sum(1,2,3,4,5,6,7,8,9);
Console.WriteLine(n); //45
Console.ReadKey();
}
}
引用參數和輸出參數:
using System;
class MyClass
{
static void proc1(ref int num)
{
num = num * num;
}
static void proc2(out int num)
{
num = 100;
}
static void Main()
{
int a = 9;
proc1(ref a); /* 引用參數(ref) 參數必須是已初始化的 */
Console.WriteLine(a); //81
int b;
proc2(out b); /* 輸出參數類似 ref(但初始化不是必要的), 是從函數中接出一個值來 */
Console.WriteLine(b); //100
Console.ReadKey();
}
}