params 關(guān)鍵字可以指定在參數(shù)數(shù)目可變處采用參數(shù)的方法參數(shù)。
在方法聲明中的 params 關(guān)鍵字之后不允許任何其他參數(shù),并且在方法聲明中只允許一個(gè) params 關(guān)鍵字。
示例
// cs_params.cs
using System;
public class MyClass
{
public static void UseParams(params int[] list)
{
for ( int i = 0 ; i < list.Length ; i++ )
Console.WriteLine(list[i]);
Console.WriteLine();
}
public static void UseParams2(params object[] list)
{
for ( int i = 0 ; i < list.Length ; i++ )
Console.WriteLine(list[i]);
Console.WriteLine();
}
public static void Main()
{
UseParams(1, 2, 3);
UseParams2(1, 'a', "test");
int[] myarray = new int[3] {10,11,12};
UseParams(myarray);
}
}
輸出
1
2
3
1
a
test
10
11
12
===============================================
方法參數(shù)上的 out 方法參數(shù)關(guān)鍵字使方法引用傳遞到方法的同一個(gè)變量。當(dāng)控制傳遞回調(diào)用方法時(shí),在方法中對(duì)參數(shù)所做的任何更改都將反映在該變量中。
當(dāng)希望方法返回多個(gè)值時(shí),聲明 out 方法非常有用。使用 out 參數(shù)的方法仍然可以返回一個(gè)值。一個(gè)方法可以有一個(gè)以上的 out 參數(shù)。
若要使用 out 參數(shù),必須將參數(shù)作為 out 參數(shù)顯式傳遞到方法。out 參數(shù)的值不會(huì)傳遞到 out 參數(shù)。
不必初始化作為 out 參數(shù)傳遞的變量。然而,必須在方法返回之前為 out 參數(shù)賦值。
屬性不是變量,不能作為 out 參數(shù)傳遞。
如果兩個(gè)方法的聲明僅在 out 的使用方面不同,則會(huì)發(fā)生重載。不過,無(wú)法定義僅在 ref 和 out 方面不同的重載。例如,以下重載聲明是有效的:
class MyClass
{
public void MyMethod(int i) {i = 10;}
public void MyMethod(out int i) {i = 10;}
}
而以下重載聲明是無(wú)效的:
class MyClass
{
public void MyMethod(out int i) {i = 10;}
public void MyMethod(ref int i) {i = 10;}
}
有關(guān)傳遞數(shù)組的信息,請(qǐng)參見使用 ref 和 out 傳遞數(shù)組。
示例
// cs_out.cs
using System;
public class MyClass
{
public static int TestOut(out char i)
{
i = 'b';
return -1;
}
public static void Main()
{
char i; // variable need not be initialized
Console.WriteLine(TestOut(out i));
Console.WriteLine(i);
}
}
輸出
-1
b
========================================
方法參數(shù)上的 ref 方法參數(shù)關(guān)鍵字使方法引用傳遞到方法的同一個(gè)變量。當(dāng)控制傳遞回調(diào)用方法時(shí),在方法中對(duì)參數(shù)所做的任何更改都將反映在該變量中。
若要使用 ref 參數(shù),必須將參數(shù)作為 ref 參數(shù)顯式傳遞到方法。ref 參數(shù)的值被傳遞到 ref 參數(shù)。
傳遞到 ref 參數(shù)的參數(shù)必須最先初始化。將此方法與 out 參數(shù)相比,后者的參數(shù)在傳遞到 out 參數(shù)之前不必顯式初始化。
屬性不是變量,不能作為 ref 參數(shù)傳遞。
如果兩種方法的聲明僅在它們對(duì) ref 的使用方面不同,則將出現(xiàn)重載。但是,無(wú)法定義僅在 ref 和 out 方面不同的重載。例如,以下重載聲明是有效的:
class MyClass
{
public void MyMethod(int i) {i = 10;}
public void MyMethod(ref int i) {i = 10;}
}
但以下重載聲明是無(wú)效的:
class MyClass
{
public void MyMethod(out int i) {i = 10;}
public void MyMethod(ref int i) {i = 10;}
}
有關(guān)傳遞數(shù)組的信息,請(qǐng)參見使用 ref 和 out 傳遞數(shù)組。
示例
// cs_ref.cs
using System;
public class MyClass
{
public static void TestRef(ref char i)
{
// The value of i will be changed in the calling method
i = 'b';
}
public static void TestNoRef(char i)
{
// The value of i will be unchanged in the calling method
i = 'c';
}
// This method passes a variable as a ref parameter; the value of the
// variable is changed after control passes back to this method.
// The same variable is passed as a value parameter; the value of the
// variable is unchanged after control is passed back to this method.
public static void Main()
{
char i = 'a'; // variable must be initialized
TestRef(ref i); // the arg must be passed as ref
Console.WriteLine(i);
TestNoRef(i);
Console.WriteLine(i);
}
}
輸出
b
b
|
新聞熱點(diǎn)
疑難解答