By default, the Equals (object o) method of the Object (provided by the Base Class object) is used to compare whether two Object variables reference the same object.
We must define our own object comparison methods.
The IComparable and ICompare interfaces are the standard methods for comparing objects in. net framework. The differences between these two interfaces are as follows:
1. IComparable is implemented in the class of the object to be compared. You can compare this object with another object.
2. IComparer is implemented in a separate class and can compare any two objects.
Generally, we use IComparable to provide the default comparison code of the class, and use other classes to provide non-default comparison code.
1. IComparable provides the int CompareTo (object obj) method ). This method accepts an object, so this interface can be implemented.
For example, in order to send the Person object to it,
Indicates whether this person is older or younger than the current person. In fact, this method returns an int, so the following code shows that the second person is older or younger.
Copy codeThe Code is as follows: if (person1.CompareTo (person2) = 0)
{
Console. WriteLine ("Same age ");
}
Else if (person1.CompareTo (person2)> 0)
{
Console. WriteLine ("person 1 is older ");
}
Else
{
Console. WriteLine ("person1 is younger ");
}
2. IComparer also provides the Compare () method. This method accepts two objects and returns an integer result, which is the same as CompareTo.
For objects that support IComparer, use the following code:Copy codeThe Code is as follows: if (personComparer. Compare (person1, person2) = 0)
{
Console. WriteLine ("same age ");
}
Else if (personComparer. Compare (person1, person2)> 0)
{
Console. WriteLine ("person 1 is older ");
}
Else
{
Console. WriteLine ("person1 is younger ");
}
In both cases, the parameter provided to the method is of the system. object type. That is to say, two objects of any type can be compared. Therefore, some types of comparison are usually required before the returned results. If an error type is used, an exception is thrown. In fact, we use the generic interface IComparable <T> to omit object conversion. You can refer to the following diary.
Iii.. net framework provides the default implementation method of the IComparer interface on the Comparer class. The Comparer class is located in the system. collections namespace, which can be used for simple types and support for IComparable
Compare any types of interfaces with specific cultures.For example, you can use the following code:Copy codeThe Code is as follows: string firststring = "First String ";
String secondstring = "Second string ";
Comparer. Default. Compare (firststring, secondstring );
Int firstNumber = 35;
Int secondNumber = 23;
Comparer. Default. Compare (firstNumber, secondNumber );
Here, a Comparer. Default static member is used to obtain an instance of the Comparer class, and then compared using the Compare () method.
When using Comparer, you must use comparable types. For example, if you try to compare firstString and firstNumber, an exception is generated.
The following are some notes for this class:
1. Check the objects sent to Comparer. Compare () to see if they support IComparable. If so, use this implementation code.
2. A null value is allowed, indicating that it is smaller than other objects.
3. the string is processed according to the current culture. To process strings according to different cultures (or languages), The Comparer class must use its constructor for instance to transmit the System. Globalization. CultureInfo object of the specified culture.
4. strings must be case-sensitive during processing. To process strings in case-insensitive mode, use the CaseInsensitiveComparer class, which works in the same way.