ref:
namespace TestRefP
{
using System;
public class myClass
{
public static void RefTest(ref int iVal1 )
{
iVal1 += 2;
}
public static void Main()
{
int i=3; //变量需要初始化
RefTest(ref i );
Console.WriteLine(i);
}
}
}
必须注意的是变量要须先初始化。
结果:
5
out:
public class mathClass
{
public static int TestOut(out int iVal1, out int iVal2)
{
iVal1 = 10;
iVal2 = 20;
return 0;
}
public static void Main()
{
int i, j; // 变量不需要初始化。
Console.WriteLine(TestOut(out i, out j));
Console.WriteLine(i);
Console.WriteLine(j);
}
}
结果:
0 10 20
参数数列:
using System;
class Test
{
static void F(params int[] args) {
Console.WriteLine("# 参数: {0}", args.Length);
for (int i = 0; i < args.Length; i++)
Console.WriteLine("\targs[{0}] = {1}", i, args[i]);
}
static void Main() {
F();
F(1);
F(1, 2);
F(1, 2, 3);
F(new int[] {1, 2, 3, 4});
}
}
以下为输出结果:
# 参数: 0
# 参数: 1
args[0] = 1
# 参数: 2
args[0] = 1
args[1] = 2
# 参数: 3
args[0] = 1
args[1] = 2
args[2] = 3
# 参数: 4
args[0] = 1
args[1] = 2
args[2] = 3
args[3]
注意使用params和不使用的区别:
使用:就是参数数列。可以传递任意个参数。
不使用:只能传递数组。