復制代碼 代碼如下:
 
public partial class Form1 : Form 
{ 
public static void UseParams(params int[] list) 
{ 
string temp = ""; 
for (int i = 0; i < list.Length; i++) 
temp = temp +" " +list[i].ToString(); 
MessageBox.Show(temp); 
} 
public static void UseParams2(params object[] list) 
{ 
string temp = ""; 
for (int i = 0; i < list.Length; i++) 
temp = temp + " " + list[i].ToString(); 
MessageBox.Show(temp); 
} 
public Form1() 
{ 
InitializeComponent(); 
} 
private void button1_Click(object sender, EventArgs e) 
{ 
UseParams(1, 2, 3);//看參數是3個 
UseParams(1, 2); //看參數是2個,可變吧 
UseParams2(1, 'a', "test"); 
int[] myarray = new int[3] { 10, 11, 12 }; 
UseParams(myarray); //看也可以是容器類,可變吧:) 
} 
} 
復制代碼 代碼如下:
 
class MyClass 
{ 
public void MyMethod(int i) {i = 10;} 
public void MyMethod(out int i) {i = 10;} 
} 
復制代碼 代碼如下:
 
class MyClass 
{ 
public void MyMethod(out int i) {i = 10;} 
public void MyMethod(ref int i) {i = 10;} 
} 
復制代碼 代碼如下:
 
class MyClass 
{ 
public void MyMethod(int i) {i = 10;} 
public void MyMethod(ref int i) {i = 10;} 
} 
復制代碼 代碼如下:
 
class MyClass 
{ 
public void MyMethod(out int i) {i = 10;} 
public void MyMethod(ref int i) {i = 10;} 
} 
復制代碼 代碼如下:
 
public static string TestOut(out string i) 
{ 
i = "out b"; 
return "return value"; 
} 
public static void TestRef(ref string i) 
{ 
//改變參數 
i = "ref b"; 
} 
public static void TestNoRef(string refi) 
{ 
// 不用改變任何東西,這個太明顯了 
refi = "on c"; 
} 
public Form1() 
{ 
InitializeComponent(); 
} 
private void button1_Click(object sender, EventArgs e) 
{ 
string outi;//不需要初始化 
MessageBox.Show(TestOut(out outi));//返回值 
//輸出"return value"; 
MessageBox.Show(outi);//調用后的out參數 
//輸出"out b"; 
string refi = "a"; // 必須初始化 
TestRef(ref refi); // 調用參數 
MessageBox.Show(refi); 
//輸出"ref b"; 
TestNoRef(refi);//不使用ref 
MessageBox.Show(refi); 
//輸出"ref b"; 
} 
新聞熱點
疑難解答
圖片精選