using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace RefAndOutAndParams { class Program { /// <summary> /// 一个方法中可以动态传递多个参数的方法 /// </summary> /// <param name="list"></param> public static void MyTestParams(params Object [] list){ for (int i = 0; i < list.Length; i++) { Console.WriteLine(list[i]); } } /// <summary> /// 单个Ref的方法,方法中Ref传递的参数,在传递进方法之前,必须要有初始值,Ref相当于是一个变量的引用 /// </summary> /// <param name="temp"></param> public static void MyTestRef(ref int temp) { temp = temp + 100; } /// <summary> /// 无Ref的方法, /// </summary> /// <param name="temp"></param> public static void NoRef(int temp) { temp = temp + 100; } public static void NoRefStr(string str) { str = "Tomfanxiaojun"; } /// <summary> /// 多个Ref参数 /// </summary> /// <param name="temp1"></param> /// <param name="temp2"></param> public static void MyTestRefs(ref int temp1, ref int temp2) { temp1 = temp1 + 1; temp2 = temp2 * 2; } /// <summary> /// 单个的out参数的方法,该参数在传递这个方法之前,只要定义,不要赋初始值 /// </summary> /// <param name="temp1"></param> /// <param name="a"></param> public static void MyTestOut(out int temp1,int a) { temp1 = 9+a; } public static void MyTestOuts(out int temp1,out int temp2, int a) { temp1 = 9 + a; temp2 = a * 9; } static void Main(string[] args) { //动态参数方法的调用 MyTestParams("OK1", "OK2", 1); int a = 200; //正常方法的调用 NoRef(a); Console.WriteLine(a); //单个Ref参数方法的调用 MyTestRef(ref a); Console.WriteLine(a); MyTestRef(ref a); Console.WriteLine(a); int temp1 = 10; int temp2 = 20; MyTestRefs(ref temp1,ref temp2); Console.WriteLine(temp1); Console.WriteLine(temp2); int outvalue; MyTestOut(out outvalue, 12); Console.WriteLine(outvalue); int outvalue1,outvalue2; MyTestOuts(out outvalue1,out outvalue2, 33); Console.WriteLine(outvalue1); Console.WriteLine(outvalue2); string str = "Jake"; NoRefStr(str); Console.WriteLine(str); Console.ReadLine(); } } }