复制代码 代码如下:
 
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"; 
} 
新闻热点
疑难解答
图片精选