引用类型的赋值函数

来源:互联网 发布:淘宝网棒球服女款 编辑:程序博客网 时间:2024/05/19 13:19

 

// For reference types, assignment copies the reference

// For value types, assignment copies the value

            string a = "a";

            string b = a;

            string c = b;

 

//string.Equals compares with value

//object.ReferenceEquals decides whether the same instance

            bool eq0 = b.Equals(a);//true

            bool eq1 = object.ReferenceEquals(a, b);//true

 

            bool eq2 = c.Equals(a);//true

            bool eq3 = object.ReferenceEquals(a, c);//true

 

//The two "a" constants use the same instance.

            a = "a";

 

            bool eq4 = b.Equals(a);//true

            bool eq5 = object.ReferenceEquals(a, b);//true

 

            bool eq6 = c.Equals(a);//true

            bool eq7 = object.ReferenceEquals(a, c);//true

 

            a = new System.String(new char[] { 'a' });

 

            bool eq8 = b.Equals(a);//true

            bool eq9 = object.ReferenceEquals(a, b);//false

 

            bool eq10 = c.Equals(a);//true

            bool eq11 = object.ReferenceEquals(a, c);//false

 

            bool eq12 = c.Equals(b);//true

            bool eq13 = object.ReferenceEquals(b, c);//true

 

            Object a0 = new Object();

            Object a1 = a0;

 

            bool eq30 = a1.Equals(a0);//true

            bool eq31 = object.ReferenceEquals(a1, a0);//true

 

            a0 = new Object();

 

            bool eq32 = a1.Equals(a0);//false

            bool eq33 = object.ReferenceEquals(a1, a0);//false

 

原创粉丝点击