ref用于类类型

时间:2022-12-28 15:18:10

static void Main(string[] args)         {

Fruit f = new Fruit()      {

    Weight = 1,

    Color = ""

    };

Change(ref f);

//把引用对象作为参数传进一个方法,实际上是在栈上新分配了一块内存保存传入的地址

    WriteLine(f.Weight); //output is 2

   }

static void Change(ref Fruit f)
        {
            f.Weight = 2;
            f = new Fruit() { Weight = 3 };
        }

https://www.cnblogs.com/lwzz/archive/2012/03/11/2390589.html