skip to main |
skip to sidebar
IComparable Vs IComparer
When we create list<> over native types we don’t need to have implement any interface. But when we have custom types (Booking, Invoice, etc) we need to give hint to compiler about how two objects should be compared. IComparable and IComparer are the interface which we can implement and give compiler our definition of comparing two objects.
IComparable example :
IComparer example :
Difference : Here in this case we have created class Person and we have full control over it. So it’s better to implement IComparable. If Person class was 3rd party and we have received it as assembly, we can’t modify the class definition. So in that case we can create separate class called PersonComparer and implement IComparer on it.
No comments:
Post a Comment