Object comparison and sorting (2): Implement icomparable and icomparer interfaces and comparer class

Source: Internet
Author: User

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 give a default comparison of classes.Code, 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.

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:

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 type of comparison is usually required before the returned results. If an error occurs

And throws an exception.In fact, we use the generic interface icomparable <t> to omit object conversion. You can refer to the following diary.

 

Iii.. NET FrameworkComparer classProvides the default implementation method of the icomparer interface. The class comparer is located in the system. Collections namespace.

Compare any types of interfaces with specific cultures. For example, you can use 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 );

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

System. Globalization. cultureinfo object.

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.

 

The next diary will introduce the use of these two interfaces for sorting.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.