Wednesday, January 1, 2014

Using IComparer, Comparer and IComparer in C#

IComparer

IComparer interface exposes a method to compare two objects. This interface comes under System.Collections namespace. You can use this with Array.Sort() method. To use with Generic classes you must use IComparer<T> interface.

You can check the example in MSDN documentation to see how to use IComparer with an Array.

IComparer<T>

This interface comes under System.Collections.Generic namespace. This is similar to IComparer. This interface is used with List<T>.Sort and List<T>.BinarySearch methods and provides a way to customize the sort order of a collection.

The StringComparer class implements this for String. 

IEqualityComparer<T>

Comparer<T>

This interface provides a base class for implementations of the IComparer<T> interface. 

It is recommended to use Comparer<T> instead of IComparer<T> 


Resources

0 comments:

Post a Comment

Powered by Blogger.


Software Architect at Surge Global/ Certified Scrum Master

Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

Search This Blog

Facebook