注册 登录
编程论坛 C# 论坛

新人报道 对象作为值传递疑惑

扫地大爷 发布于 2021-05-21 07:36, 1399 次点击
namespace 引用类型对象作为值参数传递_看书不能理解_
{
    class Myss {

        public int Myss1 = 20;

}
    class Program
    {
        static void Test(Myss f1) {
            f1.Myss1 = 50;
            Console.WriteLine($"After member assigment; {f1.Myss1}");
            f1 = new Myss();
            Console.WriteLine($"After new object creation {f1.Myss1}");

        }

        static void Main(string[] args)
        {
            Myss a1 = new Myss();
            Console.WriteLine($"Before method call; {a1.Myss1}");
            Test(a1);
            Console.WriteLine($"afore method call; {a1.Myss1}");
            Console.ReadKey();
        }
    }
}
为什么a.Myss1==50
2 回复
#2
zbjzbj2021-06-05 16:42
static void Main(string[] args)
        {
            Myss a1 = new Myss();
            Console.WriteLine($"Before method call; {a1.Myss1}");
            Test(a1);{此调用运行的是 Test(f1),只是把a1的各个值传递给了f1,在此只有一个,就是f1.Myss1=a1.Myss1(赋值),但是a1还在原位置呆着,子程序运行结束,f1寿命终结}
            Console.WriteLine($"afore method call; {a1.Myss1}");
            Console.ReadKey();
        }
#3
扫地大爷2021-06-16 22:38
回复 2楼 zbjzbj
多谢大佬指点
1