// 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");
留言列表