Monday, April 16, 2012

Advantage of abstract class over interface

Advantage of abstract class over interface:

Consider versioning:

·         You cannot add members to interface without breaking the existing code.  
·         But you can safely add new members to abstract classes.



Choosing between interface & abstract class:

·         Use interface when you want to implement multiple inheritance.
·         Use interface for decoupling
·         Use abstract class in case of frequent versioning scenario.

Compare 2 generic list

How to Compare 2 generic lists:

            List<int> list1 = new List<int> { 1, 2, 3, 5, 6 };
            List<int> list2= new List<int> { 1, 2, 3 };


Answer: if you are trying to see if 2 sets are equal, then the best approach is to check the except extension method on Enumerable class. And make sure that the result does not have any entries.


            var except = Enumerable.Except(list1, list2);

            if (except.Count() > 0)
                Console.Write("Not equal");
            else
                Console.Write("equal");;

Reference: