using System; using System.Collections.Generic; using System.Text; namespace V6._引用传递ref { class Person { private string name; public string Name { get { return name; } set { name = value; } } } class Program { //值类型赋值的时候将栈中的数据拷贝了一个副本 // 引用传递时传递堆本身的地址 static void Main(string[] args) { //ref int n = 100; Ml(ref n); Console.WriteLine(n); //101 Person p1=new Person (); p1.Name ="黄"; M2(ref p1); Console.WriteLine(p1.Name); //out 传递的是栈的地址 int m ; int n; TestOut(out m, out n); Console.WriteLine(m); //params Console.ReadKey(); } //ref 传递的是栈的地址 static void Ml(ref int n) { n = n + 1; } static void M2(ref Person p2) { // ref Person p2 引用传递不会对栈开辟一个变量 共用一个地址 0xx01 p1 p2 公用同一个栈 // p2=new Person (); 创建一个新的对象 在堆开辟一个空间 地址 0xx02 p1 p2 因公用同一个栈, p2指定修改后 p1也跟着修改 p2=new Person (); p2.Name ="金"; } //out 让函数可以输出多个值 //1 out参数的变量在传递之前不需要赋值,即使赋值了也不能在方法中使用 //2. 在方法中必须为 out参数值 static void TestOut(out int m,out int n) { m = 100; n = 99; } //params 可变参数 //1.如果方法有多个参数,可变参数必须作为最后一个参数 //2.可变参数可以传递参数也可以不传递参数,如果不传递参数,则args数组为一个长度为0的数组 //3.可变参数可以直接传递一个数组进来 static void TestParams(string msg, params int[] args) { if (args != null) { } } } } |
|
来自: 时间剧毒 > 《第十章:接口、抽象与密封》