简明的说明下ref 的关键核心地方

来源:互联网 发布:销售信心 知乎 编辑:程序博客网 时间:2024/05/01 17:35

直接代码说明

 protected void Page_Load(object sender, EventArgs e)        {            string test = "我学编程的";            TestString1(test);            IntResult.Text = test;            //我学编程的            TestString2(ref test);            IntResult2.Text = test;            //我学编程的小菜鸟            Article articleA = null;            Article articleB = null;            Fct(articleA, ref articleB);            IntResult.Text = string.Format("articleA:{0};articleB:{1}",(articleA==null),(articleB==null));            //articleA:True;articleB:False             Article articleC = new Article() ;            articleC.Price = 3;            Article articleD = new Article();            articleD.Price = 3;            Fct(articleC, ref articleD);            IntResult2.Text = string.Format("articleC Price:{0};articleD Price:{1}", articleC.Price, articleD.Price);            //articleC Price:5;articleD Price:5        }        public void TestString1(string a)        {            a += "高手";        }        public void TestString2(ref string a)        {            a += "小菜鸟";        }        public void Fct(Article a, ref Article b)        {            if (a == null)                a = new Article();            else                a.Price = 5;            if (b == null)                b = new Article();            else                b.Price = 5;        }        public class Article        {            public int Price { get; set; }        }
ref 强于默认的值类型按值传递,引用类型按引用传递。

0 0