值方式参数传递:
1.使用引用类型作为参数
class Voter{ public void Vote(Se se){ se.Popularity++; } } //测试 Se zhang=new Se(); zhang.age=25; zhang.Popularity=10; //投票前 MessageBox.show(Se.Popularity); Voter voter = new Voter(); voter.Vote(zhang); //应用类型作为 参数 //投票后 MessageBox.show(Se.Popularity)
分析结果可知:当引用变量发生变化时,参数发生了变化,因此 当类作为参数传递时,参数被修改,类成员的值被修改,数组也是。
2,使用值类型作为参数
struct Se{ public int pro{get;set} //d定义一个结构 } class Voter{ public void Vote(Se se){ se.Popularity++; } } //测试 Se zhang zhang.pro=10; //投票前 MessageBox.show(Se.Popularity); Voter voter = new Voter(); voter.Vote(zhang); //应用类型作为 参数 //投票后 MessageBox.show(Se.Popularity)
得出结果 :使用值类型作为参数,当发生变化时,参数不被修改。