06判等对象是否相等

  判断相等的3个方法

实例方法

public virtual bool Equals(object obj) 
{ 
    return RuntimeHelpers.Equals(this, obj); 
}

比较值类型静态方法

public static bool Equals(object objA, object objB) 
{ 
    return ((objA == objB) || (((objA != null) && (objB != null)) && objA.Equals(objB))); 
}

比较引用类型静态方法

public static bool ReferenceEquals(object objA, object objB) 
{ 
    return (objA == objB); 
}

 

  判断引用类型是否相等

    class Program 
    { 
        static void Main(string[] args) 
        { 
            Team t1 = new Team("马尔切洛·里皮"); 
            Team t2 = new Team("马尔切洛·里皮"); 
            var result = (t1 == t2); 
            Console.WriteLine(result); 
            result = t1.Equals(t2); 
            Console.WriteLine(result); 
            Console.ReadKey(); 
        } 
    }
 
    public class Team 
    { 
        public string _coach = string.Empty;
 
        public Team(string coach) 
        { 
            this._coach = coach; 
        } 
    }
 
    public struct TeamStruct 
    { 
        public string _coach;
 
        public TeamStruct(string coach) 
        { 
            this._coach = coach; 
        } 
    }
 

结果:
false
false

分析:引用类型比较的是引用地址,由于t1和t2指向不同的对象实例,所以dou都返回false。   

 

  判断值类型是否相等

□ 值类型判断方法

派生于System.ValueType,对System.Object中的虚方法Equals(object obj)进行了重写

public override bool Equals(object obj) 
{ 
    if (obj == null) 
    { 
        return false; 
    } 
    RuntimeType type = (RuntimeType) base.GetType(); 
    RuntimeType type2 = (RuntimeType) obj.GetType(); 
    if (type2 != type) //比较两个对象是否是同一类型 
    { 
        return false; 
    } 
    object a = this; 
    if (CanCompareBits(this)) //对象成员如果存在对于堆的引用返回false 
    { 
        return FastEqualsCheck(a, obj); 
    } 
    //反射获取值类型的所有字段 
    FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); 
    for (int i = 0; i < fields.Length; i++) //遍历字段,对各个字段进行比较 
    { 
        object obj3 = ((RtFieldInfo) fields[i]).UnsafeGetValue(a); 
        object obj4 = ((RtFieldInfo) fields[i]).UnsafeGetValue(obj); 
        if (obj3 == null) 
        { 
            if (obj4 != null) 
            { 
                return false; 
            } 
        } 
        else if (!obj3.Equals(obj4)) 
        { 
            return false; 
        } 
    } 
    return true; 
}

□ 用==判断是否相等

        static void Main(string[] args) 
        { 
            TeamStruct ts1 = new TeamStruct("马尔切洛·里皮"); 
            TeamStruct ts2 = ts1; 
            var result = (ts1 == ts2); 
            Console.WriteLine(result); 
            Console.ReadKey(); 
        }

出现编译错误。原因是值类型不能用==进行判断。

□ 用Equals()实例方法判断是否相等

        static void Main(string[] args) 
        { 
            TeamStruct ts1 = new TeamStruct("马尔切洛·里皮"); 
            TeamStruct ts2 = ts1; 
            var result = ts1.Equals(ts2); 
            Console.WriteLine(result); 
            Console.ReadKey(); 
        }

返回true。可见,如果值类型的字段相等,那就相等。

        static void Main(string[] args) 
        { 
            TeamStruct ts1 = new TeamStruct("马尔切洛·里皮"); 
            TeamStruct ts2 = ts1; 
            ts2._coach = "高洪波"; 
            var result = ts1.Equals(ts2); 
            Console.WriteLine(result); 
            Console.ReadKey(); 
        }

返回false,当然,值类型的字段有不相等,就会整个不相等。

 

  判断复杂值类型是否相等

即值类型中包含引用类型和值类型。

   class Program 
    { 
        static void Main(string[] args) 
        { 
            Team t = new Team("马尔切洛·里皮"); 
            TeamStruct ts = new TeamStruct("马尔切洛·里皮");
 
            NationalTeam nt1 = new NationalTeam(t, ts); 
            NationalTeam nt2 = nt1; 
            var result = nt1.Equals(nt2); 
            Console.WriteLine(result); 
            Console.ReadKey(); 
        } 
    }
 
    public class Team 
    { 
        public string _coach = string.Empty;
 
        public Team(string coach) 
        { 
            this._coach = coach; 
        } 
    }
 
    public struct TeamStruct 
    { 
        public string _coach;
 
        public TeamStruct(string coach) 
        { 
            this._coach = coach; 
        } 
    }
 
    public struct NationalTeam 
    { 
        public Team _team; 
        public TeamStruct _structTeam;
 
        public NationalTeam(Team team, TeamStruct structTeam) 
        { 
            this._team = team; 
            this._structTeam = structTeam; 
        } 
    }
 

返回true,会遍历比较引用类型成员和值类型成员。在nt1和nt2中,类型为Team的引用类型成员_team指向同一个对象实例,  类型为TeamStruct的值类型成员_structTeam相等,所有整个返回。如图:


参考资料
《.NET之美》--张子阳,感谢写了这么好的书!

posted @ 2014-03-14 10:38  Darren Ji  阅读(442)  评论(0编辑  收藏  举报

我的公众号:新语新世界,欢迎关注。