Saturday, May 8, 2010

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.

Wednesday, May 5, 2010

Static Class Vs Singleton

1) Singleton preserves conventional class approache and dont require that you use the static keyword everywhere.
2) Unlike static classes, we can use singleton as parameters or objects.
3) Singleton class can implement interface whereas static class can not.