学习了C# 方法,总结一下方法的三个关键参数
1 out
定义为 out 的参数 必须在方法中赋值,不必再方法外赋值
定义为 out 参数,在方法的声明和调用中都要写 out 关键字,
在方法内部不先赋值,无法参与运算
方法的声明
static void M(int a ,out int b)
{
....
}
//方法的调用
int n1 =1;
int n2;
M(n1, out n2);
2 ref 定义引用参数
定义为 ref 参数,在方法的声明和调用中都要写 ref 关键字,
用作 ref 参数的变量不能是常量,且必须使用初始化过的变量
ref 定义的参数要在方法外赋值,可不再方法赋值
<pre class="html" name="code">static void M( ref int a ,ref int b)
{
...
}
//方法的调用
int n1=1;
int n2 =3;
m(ref n1 ,ref n2);
3 params
params 定义的参数只能在参数列表的最后,个数几乎不受限制
// 方法的声明
static void M( int a ,int b ,params int[] c)
{
}
// 方法的调用
int n1=1;
int n2 =3;
int[] n3 ={1,2,3,4,5,6};
M(n1,n2,n3);
M(n1,n2,1,2,3,4,5,6,7,8,9);