首页 > 编程 > C# > 正文

C#判等对象是否相等的方法汇总

2020-01-24 02:32:13
字体:
来源:转载
供稿:网友

本文以实例形式展示了C#判等对象是否相等的常用方法,非常实用,可供大家参考借鉴之用。具体分析如下:

一、判断相等的3个方法

1.实例方法

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

2.比较值类型静态方法

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

3.比较引用类型静态方法

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。  

三、判断值类型是否相等

1.值类型判断方法

派生于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; }

2.用==判断是否相等

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

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

3.用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,当然,值类型的字段有不相等,就会整个不相等。

4.判断复杂值类型是否相等

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

  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相等,所有整个返回。如下图所示:

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表