When it comes to ranking, comparison is bound to come to mind. What is the relationship between. NET framework and comparison? Of course, IComparable, IComparable < T >, IComparer < T >, IEquality Comparer < T > are the interfaces. IComparable < T > is a generic version of IComparable. The advantages of generics naturally go without saying much. In all possible cases, we should give priority to generics. Lao Zhao's This article Answer your questions about generic performance.
IComparable, IComparable<T>, IComparer<T>, IEquality Comparer<T> in.NET framework
They are defined as follows:
public interface IComparer<T> { int Compare(T x, T y); } public interface IComparable { int CompareTo(object obj); } public interface IComparable<T> { int CompareTo(T other); } public interface IEqualityComparer<T> { bool Equals(T x, T y); int GetHashCode(T obj); }
For a specific type of IComparer < T > interface is implemented, so when you implement sorting algorithm, you can call IComparer < T > Compare (T x, T y) to compare instance X and y. The method return value is less than 0, indicating that x is less than y, if return value is equal to 0, indicating that x is equal to y, if return value is greater than 0, indicating that x is greater than y. For IComparable < T > interface, when a certain type of interface is implemented, the CompareTo(T other) method can be invoked on this type of instance. The return value of the method is less than 0, indicating that the instance is less than the other parameter. If the return value is equal to 0, it indicates that the instance is equal to the other parameter, and if the return value is greater than 0, it indicates that the instance is larger than the other parameter. For the IEquality Comparer < T > interface, you can call the Equals(T x, T y) method according to the implementation class of the interface, and return true to indicate that X and y are equal.
By implementing the interface IComparable <Employee>:
Now we are going to prepare an employee class with three members (age, name, department) to judge the size according to the age. The older employees are larger than the younger ones. The code is as follows:
public class Employee : IComparable<Employee> { public int Age { get; set; } public string Name { get; set; } public string Department { get; set; } public Employee(int age,string name,string department) { this.Age = age; this.Name = name; this.Department = department; } #region IComparable<Employee> Members public int CompareTo(Employee other) { return this.Age - other.Age; } #endregion }
Test code:
static void Main(string[] args) { Employee anders = new Employee(35, "Anders Wang", "Developer Division"); Employee jeffrey = new Employee(28, "Jeffrey Chen", "Developer Division"); Employee steve = new Employee(35, "Anders Wang", "Developer Division"); Console.WriteLine(anders.CompareTo(jeffrey)); //output > 0 }
As you can see from Employee's ComppareTo, because Anders'Age > Jeffrey's Age, anders.CompareTo(jeffrey) returns 1, indicating that instance Anders is larger than instance jeffrey.
By implementing the interface IComparer < Employee>:
The code is as follows:
class EmployeeComparer:IComparer<Employee> { #region IComparer<Employee> Members public int Compare(Employee x, Employee y) { return x.Age - y.Age; } #endregion }
Test code:
static void Main(string[] args) { Employee anders = new Employee(35, "Anders Wang", "Developer Division"); Employee jeffrey = new Employee(28, "Jeffrey Chen", "Developer Division"); Employee steve = new Employee(35, "Anders Wang", "Developer Division"); Console.WriteLine(new EmployeeComparer().Compare(anders, jeffrey)); //output > 0 }
The above code also implements the comparison function.
By implementing the interface IEquality Comparer <Employee>:
To determine whether two objects are equal? We can implement the IEquality Comparer < T > interface or call the Equals method of the base class object. However, in most cases, the Equals method calling the base class object needs to be coerced and contains type insecurity. Next, we use Employee Equality Comparer to implement IE quality Comparer <T>. The code is as follows:
public class EmployeeEqualityComparer:IEqualityComparer<Employee> { #region IEqualityComparer<Employee> Members public bool Equals(Employee x, Employee y) { if (x.Age == y.Age && x.Name == y.Name && x.Department == y.Department) { return true; } return false; } public int GetHashCode(Employee obj) { //Because it's based on Age,Name,Department. //Therefore, the process of obtaining hash value should also be modified. //Ensure that the hash value of the same object is equal. //Override the GetHashCode method inside the type Employee. return obj.GetHashCode(); } #endregion }
Test code:
static void Main(string[] args) { Employee anders = new Employee(35, "Anders Wang", "Developer Division"); Employee jeffrey = new Employee(28, "Jeffrey Chen", "Developer Division"); Employee steve = new Employee(35, "Anders Wang", "Developer Division"); Console.WriteLine(new EmployeeEqualityComparer().Equals(anders, jeffrey)); //output false Console.WriteLine(new EmployeeEqualityComparer().Equals(anders, steve)); //output true Console.ReadKey(); }
The above code illustrates how to judge the equality of two objects by implementing the IEquality Comparer < T > interface.
summary
NET framework provides IComparable, IComparable < T >, IComparer < T >, IEquality Comparer < T >, these interfaces have a direct role in the implementation of sorting algorithm, with the help of generics, you can write a general sorting algorithm, just need you to implement the methods in the above interfaces according to specific types. It is because of the interface that we all have the protocol, which makes it possible to implement the general sorting algorithm. In addition, when dealing with data structures related to collections, such as dictionaries, hash tables, etc. When you override the Equals method of the key type because of the specific semantics, you must also override the GetHashCode method.
Reprinted at: https://www.cnblogs.com/Devfly/archive/2009/08/13/prerequisite-knowledge-about-sorting-algorithm.html