求教C#参数传递时,传递应用类型时,加不加ref的区别,有具体实例

 protected void Page_Load(object sender, EventArgs e)
        {
            A a = new A();
            a.a = "ha";
            set(a);
            TextBox1.Text = a.a; // 值为 hha

            A b = new A();
            b.a = "ha";
            set(ref b);
            TextBox2.Text = b.a; // 值为 sdfsdf
        }

        private void set(A a)
        {
            a.a = "hha";
            a = new A();
            a.a = "sdfsdf";
            TextBox1.Text = a.a;
        }

        private void set(ref A a)
        {
            a.a = "hha";
            a = new A();
            a.a = "sdfsdf";
            TextBox2.Text = a.a;
        }

 

 public class A
    {
        public string a;
    }

求教为什么加不加ref 2次运行的结果不同?引用类型的参数传递,加不加ref具体区别是什么?

你可能感兴趣的:(参数传递)