params 关键字可以指定采用数目可变的参数的方法参数 。可以发送参数声明中所指定类型的逗号分隔的参数列表或指定类型的参数数组。还可以不发送参数。
在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个
params 关键字。
public class MyClass { public static void UseParams(params int[] list) { for (int i = 0; i < list.Length; i++) { Console.Write(list[i] + " "); } Console.WriteLine(); } public static void UseParams2(params object[] list) { for (int i = 0; i < list.Length; i++) { Console.Write(list[i] + " "); } Console.WriteLine(); } static void Main() { // You can send a comma-separated list of arguments of the // specified type. UseParams(1, 2, 3, 4); UseParams2(1, 'a', "test"); // A params parameter accepts zero or more arguments. // The following calling statement displays only a blank line. UseParams2(); // An array argument can be passed, as long as the array // type matches the parameter type of the method being called. int[] myIntArray = { 5, 6, 7, 8, 9 }; UseParams(myIntArray); object[] myObjArray = { 2, 'b', "test", "again" }; UseParams2(myObjArray); // The following call causes a compiler error because the object // array cannot be converted into an integer array. //UseParams(myObjArray); // The following call does not cause an error, but the entire // integer array becomes the first element of the params array. UseParams2(myIntArray); } } /* Output: 1 2 3 4 1 a test 5 6 7 8 9 2 b test again System.Int32[]
ref 关键字使参数按引用传递。 其效果是,当控制权传递回调用方法时,在方法中对参数的任何更改都将反映在该变量中。
不要将“通过引用传递”概念与“引用类型”概念相混淆。这两个概念不相关;方法参数无论是值类型还是引用类型,都可通过
ref 进行修饰。 因此,通过引用传递值类型时没有值类型装箱。
若要使用 ref 参数,则方法定义和调用方法都必须显式使用
ref 关键字。 例如:
class RefExample { static void Method(ref int i) { i = 44; } static void Main() { int val = 0; Method(ref val); // val is now 44 } }
传递到 ref 参数的参数必须最先初始化。 这与
out 不同,后者的参数在传递之前不需要显式初始化。 有关更多信息,请参见
out。
尽管 ref 和
out 关键字会导致不同的运行时行为,但在编译时并不会将它们视为方法签名的一部分。 因此,如果两个方法唯一的区别是:一个接受
ref 参数,另一个接受 out 参数,则无法重载这两个方法。 例如,不会编译下面的代码:
class CS0663_Example { // Compiler error CS0663: "Cannot define overloaded // methods that differ only on ref and out". public void SampleMethod(out int i) { } public void SampleMethod(ref int i) { } }
class RefOverloadExample { public void SampleMethod(int i) { } public void SampleMethod(ref int i) { } }属性不是变量。 它们实际上是方法,因此不能作为 ref 参数进行传递。
mtps:Sentence>下面的示例显示出当引用类型作为 ref 参数传递时,可以更改对象本身。
class RefExample2 { static void Method(ref string s) { s = "changed"; } static void Main() { string str = "original"; Method(ref str); Console.WriteLine(str); } } // Output: changed
out 关键字会导致参数通过引用来传递。 这与
ref 关键字类似,不同之处在于 ref 要求变量必须在传递之前进行初始化。 若要使用
out 参数,方法定义和调用方法都必须显式使用 out 关键字。 例如:
</ msdn2.microsoft.com mtps:Sentence>
class OutExample { static void Method(out int i) { i = 44; } static void Main() { int value; Method(out value); // value is now 44 } }
尽管作为 out 参数传递的变量不必在传递之前进行初始化,但被调用的方法需要在返回之前赋一个值。
尽管 ref 和 out 关键字会导致不同的运行时行为,但在编译时并不会将它们视为方法签名的一部分。 <//msdn2.microsoft.com/mtps:Sentence id=tgt7 class=tgtSentence xmlns="http://msdn2.microsoft.com/mtps" runat="server" sentenceid="7358898d11e6ee127b8b3594421cef47">因此,如果两个方法唯一的区别是:一个接受
ref 参数,另一个接受 out 参数,则无法重载这两个方法。
当希望方法返回多个值时,声明 out 方法很有用。 下面的示例使用
out 在一次方法调用中返回三个变量。 请注意,第三个参数所赋的值为 Null。这样使方法可以有选择地返回值。
class OutReturnExample { static void Method(out int i, out string s1, out string s2) { i = 44; s1 = "I've been returned"; s2 = null; } static void Main() { int value; string str1, str2; Method(out value, out str1, out str2); // value is now 44 // str1 is now "I've been returned" // str2 is (still) null; } }