c implements the IComparable IComparer interfaces and Comparer classes

  • 2020-05-12 03:11:56
  • OfStack

By default, the object's Equals(object o) method (provided by the base class Object) compares whether two object variables refer to the same object.
We have to have my own object, we have to define our own way of comparing objects.
The IComparable and ICompare interfaces are the standard way of comparing objects in.net framework. The differences between the two interfaces are as follows:
1. IComparable is implemented in the class of the object to be compared, which can be compared with another object.
2.IComparer is implemented in a single class and can compare any two objects.
In general, we use IComparable to give the default comparison code for the class, and other classes to give the non-default comparison code.
1. IComparable provides a method int CompareTo(object obj). This method accepts one object, so you can implement the interface
For example, in order to pass the Person object to it,
Indicate whether the person is older or younger than the current person. In fact, this method returns an int, so the code below indicates whether the second person is older or younger.

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 a method, Compare(). This method takes two objects and returns an integer result, which is the same as CompareTo().
For objects that support IComparer, you can use the following code:

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 parameters provided to the method are of type system.object. That is, you can compare two objects of any type. So, before returning a result, you usually need to do some type comparison and throw an exception if the wrong type is used. In fact, we are using the generic interface IComparable < T > , you can omit the object conversion. See the diary below.
3..net framework provides a default implementation of the IComparer interface on the Comparer class, which is located in the system.collections namespace and can support IComparable for simple types as well as IComparable
Culture-specific comparisons of any type of interface. For example, you can use it with the following code:

string firststring = "First String";
string secondstring = "Second string";
Comparer.Default.Compare(firststring , secondstring);

int firstNumber = 35;
int secondNumber = 23;
Comparer.Default.Compare(firstNumber , secondNumber);

The Comparer.Default static member is used to get an instance of the Comparer class and then the Compare() method is used to compare.
When using Comparer, you must use a comparable type. For example, trying to compare firstString and firstNumber generates an exception.
Here are some things to note about this class:
1. Check the objects passed to Comparer.Compare () to see if they support IComparable. If so, use the implementation code.
2. null value is allowed, which means less 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 be instantiated using its constructor to pass the System.Globalization.CultureInfo object of the specified culture.
4. Strings are case sensitive when they are processed, and if you want to handle them case insensitive, you need to use the CaseInsensitiveComparer class, which works in the same way.

Related articles: