params關鍵字可以為方法指定數目可變的參數。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[]
*/
該示例中,UseParams方法可以接受任意數目的int類型參數,UseParams2方法可以接受任意數目的object類型參數。
ref關鍵字使參數通過引用傳遞(非值傳遞)。通過引用傳遞參數的效果是:在方法中對參數的任何改變都會保留。
說明:引用傳遞和引用類型是兩個概念,不要混淆。值類型和引用類型都可以用ref修飾,當通過引用傳遞時,不會對值類型裝箱。
若要使用ref參數,方法的定義和調用都必須顯式使用ref關鍵字,如下所示:
class RefExample {
static void Method(ref int i) {
// Rest the mouse pointer over i to verify that it is an int.
// The following statement would cause a compiler error if i
// were boxed as an object.
i = i + 44;
}
static void Main() {
int val = 1;
Method(ref val);
Console.WriteLine(val);
// Output: 45
}
}
傳遞給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) { }
}
不過,有ref(out)和沒ref(out)的函數可以重載,如下所示,可以通過編譯:
class RefOverloadExample
{
public void SampleMethod(int i) { }
public void SampleMethod(ref int i) { }
}
屬性不是變量,它們是方法,所以不能作為ref參數傳遞。
下面類型的方法不能使用ref和out關鍵字:
在ref中已經對out有所介紹,下面對out參數進行詳細解析。out關鍵字可以應用在兩個地方:參數修飾符;接口或委托中修飾泛型類型參數。首先介紹參數修飾符部分。
out關鍵字通過引用傳遞參數。這與ref關鍵字相似,不過ref要求在傳遞之前初始化而變量,out沒有此要求。和ref一樣,方法定義和調用處都必須顯式使用out關鍵字。例如:
class OutExample
{
static void Method(out int i)
{
i = 44;
}
static void Main()
{
int value;
Method(out value);
// value is now 44
}
}
雖然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;
}
}
對於泛型類型參數,out關鍵字可指定該類型參數是協變的。可以在泛型接口和委托中使用out關鍵字。
通過協變,可以使用比指定類型派生程度更大的類型。這樣可以對委托類型和實現變體接口的類進行隱式轉換。引用類型支持協變和逆變,值類型不支持。
如果接口具有協變類型形參,則允許其返回派生程度更大的實參。例如,.NET framework 4中的IEnumerable<T>接口,其類型T是協變的,因此無需使用任何特殊的轉換方法就可以將IEnumerable<Of String>類型的對象分配給IEnumerable<Of Object>類型的對象。
可以向協變委托分配同一類型的其他委托,但需使用派生程序更大的泛型類型參數。
下面演示如何聲明、擴展和實現一個協變泛型接口。此外還演示如何對實現協變接口的類使用隱式轉換。
// Covariant interface.
interface ICovariant<out R> { }
// Extending covariant interface.
interface IExtCovariant<out R> : ICovariant<R> { }
// Implementing covariant interface.
class Sample<R> : ICovariant<R> { }
class Program
{
static void Test()
{
ICovariant<Object> iobj = new Sample<Object>();
ICovariant<String> istr = new Sample<String>();
// You can assign istr to iobj because
// the ICovariant interface is covariant.
iobj = istr;
}
}
在泛型接口中,當符合下列條件,可將類型參數聲明為協變的
下面演示如何聲明、實例化和調用一個協變泛型委托:
// Covariant delegate.
public delegate R DCovariant<out R>();
// Methods that match the delegate signature.
public static Control SampleControl()
{ return new Control(); }
public static Button SampleButton()
{ return new Button(); }
public void Test()
{
// Instantiate the delegates with the methods.
DCovariant<Control> dControl = SampleControl;
DCovariant<Button> dButton = SampleButton;
// You can assign dButton to dControl
// because the DCovariant delegate is covariant.
dControl = dButton;
// Invoke the delegate.
dControl();
}
在泛型委托中,如果類型僅用作方法返回類型,且不用於方法參數,則可聲明為協變的。