IEquatable<T>.Equals 是Object.Equals方法的泛型版本
2013-02-27 10:44
357 查看
The implementation of the Equals method is intended to perform a test for equality with another object of type
T, the same type as the current object. The
Equals method is called in the following circumstances:
When the Equals method is called and the
other parameter represents a strongly-typed object of type
T. (If other is of type
Object, the base
Object.Equals(Object) method is called. Of the two methods,
IEquatable<T>.Equals offers slightly better performance.)
When the search methods of a number of generic collection objects are called. Some of these types and their methods include the following:
Some of the generic overloads of the BinarySearch method.
The search methods of the List<T> class, including
List<T>.Contains(T),
List<T>.IndexOf,
List<T>.LastIndexOf, and
List<T>.Remove.
The search methods of the Dictionary<TKey, TValue> class, including
ContainsKey and
Remove.
The search methods of the generic LinkedList<T> class, including
LinkedList<T>.Contains and
Remove.
In other words, to handle the possibility that objects of a class will be stored in an array or a generic collection object, it is a good idea to implement
IEquatable<T> so that the object can be easily identified and manipulated.
两点注意:
1)如果对象需要存储在集合类中,编写这些类时要考虑实现IEquatable<T>.Equals方法以实现判等,实现IComparable<T>接口以实现排序;
2) 集合类中的某些方法例如LinkedList.Find 方法等要利用对象的Equals方法来判断是否相等。
Sample: 观察对象类的Equals, GetHashCode以及operator override
Test :
T, the same type as the current object. The
Equals method is called in the following circumstances:
When the Equals method is called and the
other parameter represents a strongly-typed object of type
T. (If other is of type
Object, the base
Object.Equals(Object) method is called. Of the two methods,
IEquatable<T>.Equals offers slightly better performance.)
When the search methods of a number of generic collection objects are called. Some of these types and their methods include the following:
Some of the generic overloads of the BinarySearch method.
The search methods of the List<T> class, including
List<T>.Contains(T),
List<T>.IndexOf,
List<T>.LastIndexOf, and
List<T>.Remove.
The search methods of the Dictionary<TKey, TValue> class, including
ContainsKey and
Remove.
The search methods of the generic LinkedList<T> class, including
LinkedList<T>.Contains and
Remove.
In other words, to handle the possibility that objects of a class will be stored in an array or a generic collection object, it is a good idea to implement
IEquatable<T> so that the object can be easily identified and manipulated.
两点注意:
1)如果对象需要存储在集合类中,编写这些类时要考虑实现IEquatable<T>.Equals方法以实现判等,实现IComparable<T>接口以实现排序;
2) 集合类中的某些方法例如LinkedList.Find 方法等要利用对象的Equals方法来判断是否相等。
Sample: 观察对象类的Equals, GetHashCode以及operator override
using System; using System.Collections.Generic; using System.Text.RegularExpressions; public class Person : IEquatable<Person> { private string uniqueSsn; private string lName; public Person(string lastName, string ssn) { this.SSN = ssn; this.LastName = lastName; } public string SSN { get { return this.uniqueSsn; } set { if (Regex.IsMatch(value, @"\d{9}")) uniqueSsn = String.Format("{0}-(1}-{2}", value.Substring(0, 3), value.Substring(3, 2), value.Substring(5, 4)); else if (Regex.IsMatch(value, @"\d{3}-\d{2}-\d{4}")) uniqueSsn = value; else throw new FormatException("The social security number has an invalid format."); } } public string LastName { get { return this.lName; } set { if (String.IsNullOrEmpty(value)) throw new ArgumentException("The last name cannot be null or empty."); else this.lName = value; } } public bool Equals(Person other) { if (other == null) return false; if (this.uniqueSsn == other.SSN) return true; else return false; } public override bool Equals(Object obj) { if (obj == null) return false; Person personObj = obj as Person; if (personObj == null) return false; else return Equals(personObj); } public override int GetHashCode() { return this.SSN.GetHashCode(); } public static bool operator == (Person person1, Person person2) { if ((object)person1 == null || ((object)person2) == null) return Object.Equals(person1, person2); return person1.Equals(person2); } public static bool operator != (Person person1, Person person2) { if (person1 == null || person2 == null) return ! Object.Equals(person1, person2); return ! (person1.Equals(person2)); } }
Test :
public class TestIEquatable { public static void Main() { // Create a Person object for each job applicant. Person applicant1 = new Person("Jones", "099-29-4999"); Person applicant2 = new Person("Jones", "199-29-3999"); Person applicant3 = new Person("Jones", "299-49-6999"); // Add applicants to a List object. List<Person> applicants = new List<Person>(); applicants.Add(applicant1); applicants.Add(applicant2); applicants.Add(applicant3); // Create a Person object for the final candidate. Person candidate = new Person("Jones", "199-29-3999"); if (applicants.Contains(candidate)) Console.WriteLine("Found {0} (SSN {1}).", candidate.LastName, candidate.SSN); else Console.WriteLine("Applicant {0} not found.", candidate.SSN); // Call the shared inherited Equals(Object, Object) method. // It will in turn call the IEquatable(Of T).Equals implementation. Console.WriteLine("{0}({1}) already on file: {2}.", applicant2.LastName, applicant2.SSN, Person.Equals(applicant2, candidate)); } } // The example displays the following output: // Found Jones (SSN 199-29-3999). // Jones(199-29-3999) already on file: True.
相关文章推荐
- C#编程:泛型中接口IEnumerable<T>中GetEnmerator方法的讨论
- 泛型限定<? extends Object>
- JS 使用List<Object> 对象中的Object对象的某个属性的临时处理方法
- C#泛型集合List<T>自带方法之排序
- C#中泛型集合List<T>反序列化问题及解决方法
- 类型安全:方法add(Object)属于原始类型List。应该将对通用类型List<E>的引用参数化
- <转>根据泛型生成数组 与数组 List相互转换的方法
- 泛型 定义在类上 、方法上、静态方法上 <?> 泛型限定 上限限定 下限限定
- eclipse环境Dynamic web module version 3.1版本的进步,简化Dynamic web object 中Servlet类的配置,不用web.xml配置<Servlet>
- KeyValuePair<TKey, TValue>是DictionaryEntry的泛型版本
- 类型“string”必须是不可以为 null 值的类型才能用作泛型类型或方法“System.Nullable<T>”中的参数“T”
- Java<>泛型方法
- 用HTML5的<DOCTYPE>标签兼容各版本IE浏览器的方法技术
- Gridview 绑定泛型List<T> 排序方法
- IE7.0以下版本列表<li>元素中元素错位解决方法
- Android Bundle 传递ArrayList<Object>的两种方法
- 7、利用泛型和反射,让public static <T> T findById(Class<T> clazz, int id)方法,更具一般性
- CentOS #include <uuid/uuid.h> 找不到文件解决方法
- java List<Map<String,Object>遍历的方法
- 返回Json数据浏览器带上<pre></pre>标签解决方法