Powered by Blogger.

Knowledge Dojo

    • Home
    • JavaScript
    • _Concepts
    • Unity
    • C#
    • About
    • Contact

    !!!!!In C#, we have been using collections like IEnumerable, ICollection, IList interchangeably but do you really know what is it that separates each other.

    They all have specific characteristics that differentiate them and makes them adaptable to certain scenarios.

    All of the above holds a list maintaining the order of items but which one should we use and in which case, let's see.

    IEnumerable

    Namespace: System.Collections

    An IEnumerable is a list or a container which can hold some items. You can iterate through each element in the IEnumerable. You can not edit the items like adding, deleting, updating, etc. instead you just use a container to contain a list of items. It is the most basic type of list container.

    All you get in an IEnumerable is an enumerator that helps in iterating over the elements. An IEnumerable does not hold even the count of the items in the list, instead, you have to iterate over the elements to get the count of items.

    An IEnumerable supports filtering elements using where clause.

    ICollection

    Namespace: System.Collections

    ICollection is another type of collection, which derives from IEnumerable and extends its functionality to add, remove, update element in the list. ICollection also holds the count of elements in it and we do not need to iterate over all elements to get the total number of elements. 

    The count of total items can be retrieved in O(1) time.

    ICollection supports enumerating over the elements, filtering elements, adding new elements, deleting existing elements, updating existing elements and getting the count of available items on the list.

    IList

    Namespace: System.Collections

    IList extends ICollection. An IList can perform all operations combined from IEnumerable and ICollection, and some more operations like inserting or removing an element in the middle of a list.

    You can use a foreach loop or a for loop to iterate over the elements.

    IQueryable

    Namespace: System.Linq

    IQueryable extends ICollection. An IQueryable generates a LINQ to SQL expression that is executed over the database layer. Instead of the generating a Func<T, bool> like the ones above, IQueryable generates an expression tree and gives Expression<Func<T, bool>> that is executed over the database layer to get data set.

    Modern languages like Java, C# doesn’t support multiple inheritance. But its predecessors like C++ supports it. Then what is the reason multiple inheritance is no longer possible now?

    What is multiple inheritance?

    In multiple inheritance, a class inherits its properties from two or more classes. This can be well explained with the help of this diagram.

    (Multiple inheritance)

    Why multiple inheritance is a headache?

    Consider the scenario where class A is the base class of class B and class C, and a fourth class D that inherits from class B and class C.
    Consider the case if class B and class C overrides the method of class A. Since class D inherits from two classes i.e. class B and class C, it creates an ambiguity which implementation of the overridden method to be used in class D.
    In this image, Class A has a method named foo().
    Class B and Class C inherits from Class A and overrides the implementation of foo() in their own way.
    But when Class D inherits from Class B and Class C, this leads to a confusion which overridden implementation to use in Class D. Whether it should be Class B: foo() or Class C: foo().

    Although C++ provides a way to solve this problem, still the solution is ambiguous to programmers in the way it behaves.
    Hence, multiple inheritance has been removed and is no more a part of Java and C#.

    Alternative to multiple inheritance

    Although, multiple inheritance is no more a part of Java and C# but still, there is a way we can implement the same along with resolving the ambiguity of the above-explained problem.

    The solution to the problem is interfaces.

    The only way to implement multiple inheritance is to implement multiple interfaces in a class.

    For this to be done for the Classes represented above, the structure would be like:



    
    
    interface A { void foo(); } interface B { void foo(); } interface C { void foo(); } Class D implements Class B,Class C { void foo() { Print(“Hello Everybody”); } }
    This way, modern languages like C#, Java can support multiple inheritance without creating a headache for developers.
    Older
    ARTICLES

    IEnumerable vs ICollection vs IList vs IQueryable in C#

    Popular Posts

    • Audio latency on Unity 3d Android Platform
    • Some weird parts of Javascript

    Created with by BeautyTemplates

    Back to top