时间:2021-07-01 10:21:17 帮助过:12人阅读
- <br>public partial class Form1 : Form <br>{ <br>public static void UseParams(params int[] list) <br>{ <br>string temp = ""; <br>for (int i = 0; i < list.Length; i++) <br>temp = temp +" " +list[i].ToString(); <br>MessageBox.Show(temp); <br>} <br>public static void UseParams2(params object[] list) <br>{ <br>string temp = ""; <br>for (int i = 0; i < list.Length; i++) <br>temp = temp + " " + list[i].ToString(); <br>MessageBox.Show(temp); <br>} <br>public Form1() <br>{ <br>InitializeComponent(); <br>} <br>private void button1_Click(object sender, EventArgs e) <br>{ <br>UseParams(1, 2, 3);//看参数是3个 <br>UseParams(1, 2); //看参数是2个,可变吧 <br>UseParams2(1, 'a', "test"); <br>int[] myarray = new int[3] { 10, 11, 12 }; <br>UseParams(myarray); //看也可以是容器类,可变吧:) <br>} <br>} <br> <br>NO.2 out <br>这是一个引用传递L。 <br>原则一:当一个方法(函数)在使用out作为参数时,在方法中(函数)对out参数所做的任何更改都将反映在该变量中。 <br>原则二:当希望方法返回多个值时,声明 out 方法非常有用。使用 out 参数的方法仍然可以返回一个值。一个方法可以有一个以上的 out 参数。 <br>原则三:若要使用 out 参数,必须将参数作为 out 参数显式传递到方法。out 参数的值不会传递到 out 参数。 <br>原则四:不必初始化作为 out 参数传递的变量,因为out 参数在进入方法(函数)时后清空自己,使自己变成一个干净的参数,也因为这个原因必须在方法返回之前为 out 参数赋值(只有地址没有值的参数是不能被.net接受的)。 <br>原则五:属性不是变量,不能作为 out 参数传递。 <br>原则六:如果两个方法的声明仅在 out 的使用方面不同,则会发生重载。不过,无法定义仅在 ref 和 out 方面不同的重载。例如,以下重载声明是有效的: <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>class MyClass <br>{ <br>public void MyMethod(int i) {i = 10;} <br>public void MyMethod(out int i) {i = 10;} <br>} <br> <br>而以下重载声明是无效的: <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>class MyClass <br>{ <br>public void MyMethod(out int i) {i = 10;} <br>public void MyMethod(ref int i) {i = 10;} <br>} <br> <br>有关传递数组的信息,请参见使用 ref 和 out 传递数组。 <br>示例附后 <br>NO.2 ref <br>ref仅仅是一个地址!!! <br>原则一:当一个方法(函数)在使用ref作为参数时,在方法中(函数)对ref参数所做的任何更改都将反映在该变量中。 <br>原则二:调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。 <br>原则三:若要使用 ref 参数,必须将参数作为 ref 参数显式传递到方法。ref 参数的值可以被传递到 ref 参数。 <br>原则四:ref参数传递的变量必须初始化,因为ref参数在进入方法(函数)时后还是它自己,它这个地址指向的还是原来的值,也因为这个原因ref参数也可以在使用它的方法内部不操作。 <br>原则六:如果两种方法的声明仅在它们对 ref 的使用方面不同,则将出现重载。但是,无法定义仅在 ref 和 out 方面不同的重载。例如,以下重载声明是有效的: <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>class MyClass <br>{ <br>public void MyMethod(int i) {i = 10;} <br>public void MyMethod(ref int i) {i = 10;} <br>} <br> <br>但以下重载声明是无效的: <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>class MyClass <br>{ <br>public void MyMethod(out int i) {i = 10;} <br>public void MyMethod(ref int i) {i = 10;} <br>} <br> <br>有关传递数组的信息,请参见使用 ref 和 out 传递数组。 <br>示例 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br>public static string TestOut(out string i) <br>{ <br>i = "out b"; <br>return "return value"; <br>} <br>public static void TestRef(ref string i) <br>{ <br>//改变参数 <br>i = "ref b"; <br>} <br>public static void TestNoRef(string refi) <br>{ <br>// 不用改变任何东西,这个太明显了 <br>refi = "on c"; <br>} <br>public Form1() <br>{ <br>InitializeComponent(); <br>} <br>private void button1_Click(object sender, EventArgs e) <br>{ <br>string outi;//不需要初始化 <br>MessageBox.Show(TestOut(out outi));//返回值 <br>//</li></ol></pre>输出"return value"; <br>MessageBox.Show(outi);//调用后的out参数 <br>//</li></ol></pre>输出"out b"; <br>string refi = "a"; // 必须初始化 <br>TestRef(ref refi); // 调用参数 <br>MessageBox.Show(refi); <br>//</li></ol></pre>输出"ref b"; <br>TestNoRef(refi);//不使用ref <br>MessageBox.Show(refi); <br>//</li></ol></pre>输出"ref b"; <br>} <br></li><li> </li><li> </li></ol></pre>