C#判等對象能否相等的辦法匯總。本站提示廣大學習愛好者:(C#判等對象能否相等的辦法匯總)文章只能為提供參考,不一定能成為您想要的結果。以下是C#判等對象能否相等的辦法匯總正文
本文以實例情勢展現了C#判等對象能否相等的經常使用辦法,異常適用,可供年夜家參考自創之用。詳細剖析以下:
1、斷定相等的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); }
2、斷定援用類型能否相等
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。
3、斷定值類型能否相等
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相等,一切全部前往。以下圖所示: