以下是我的结论:
按值传递 != 值类型
按引用(地址)传递 != 引用类型
引用类型传递的是引用的副本(可以理解为地址的副本)而ref传递的是地址
private void button1_Click(object sender, System.EventArgs e)
{
classOne a = new classOne("a");
a.i = 100;
classOne b = new classOne("b");
MySwap(a, b);
MessageBox.Show(a.str);
MessageBox.Show(a.i.ToString());
}
private void MySwap(classOne a, classOne b)
{
classOne c;
c=a;
a=b;
b=c;
MessageBox.Show(a.str);
MessageBox.Show(a.i.ToString());
}
public class classOne
{
public classOne(string s)
{
str = s;
}
public string str = "";
public int i = 0;
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
private void button1_Click(object sender, System.EventArgs e)
{
classOne a = new classOne("a");
a.i = 100;
classOne b = new classOne("b");
MySwap(ref a, b);
MessageBox.Show(a.str);
MessageBox.Show(a.i.ToString());
}
private void MySwap(ref classOne a, classOne b)
{
classOne c;
c=a;
a=b;
b=c;
MessageBox.Show(a.str);
MessageBox.Show(a.i.ToString());
}
public class classOne
{
public classOne(string s)
{
str = s;
}
public string str = "";
public int i = 0;
}