What's the difference between IEnumerable<T> and List<T> ?


1. IEnumerable is an interface, where as List is one specific implementation of IEnumerable. List is a class.

2. FOR-EACH loop is the only possible way to iterate through a collection of IEnumerable, where as List can be iterated using several ways. List can also be indexed by an int index, element can be added to and removed from and have items inserted at a particular index.

3. IEnumerable doesn't allow random access, where as List does allow random access using integral index.

4. In general from a performance standpoint, iterating thru IEnumerable is much faster than iterating thru a List.

20 comments:

  1. why class is a abstract data type?

    ReplyDelete
    Replies
    1. In Abstract class we have Data & Function Member.
      All the Function member should be inherit by the derived class.
      We need the Class to be inherit and not to use as static and instance.

      Delete
    2. The abstract classes allows us to create classes that are incomplete and needs to be implemented
      in a derived class .
      An abstract class can't be instantiated . It is just there to provide the derived classes a basic idea that what they need to implement.

      Delete
  2. very helpful. Thanks!

    ReplyDelete
  3. Here , the question is regarding why class is abstract data type?

    A Class is abstract Data Type becauses it specifies what data members and member functions(methods) contain in it(class) ,but wont provide information on how those are implemented.

    That makes Class Abstract and Class is User Defined DataType

    So Its an Abstract Data Type

    ReplyDelete
  4. If I am not wrong, using Enumerator.MoveNext() also we can traverse through a collection of IEnumerable.

    ReplyDelete

  5. is class by default public or internal

    ReplyDelete
  6. Abstract class is a class which contain both abstract & non abstract members (Abstract Methods declaration). When ever you want to implement a abstract method from abstract class you need to inherit the abstract class first & then implement the abstract method in the derived class. Abstract class should be a base class & we can not derived class as a abstract class.

    ReplyDelete
  7. Hi this is LUCKEY..........
    Let us consider there is a scenario that you have one Interface,in that there are so many defined methods(not implemented). Here you have to derive a class from that interface in this situation you need to implement all the methods of that interface but you don't know all the methods actual implementation, you know only one method implementation that you need, so for all that unknown methods you just put a key word "abstract" in your class and leave them, i hope every body know the abstract protocol, i.e if you declare one method as abstract in your class that class is an abstract class so you must declare that class as abstract class.remaining all routine things are discussed above by our friends............


    Thanks&regards
    ------------------------
    Luckey

    ReplyDelete
  8. Very used full interview questions.

    ReplyDelete
  9. If i declare two variables with same name and same datatype in partial class is it return any errors if so what kind of errors those are?

    ReplyDelete
  10. same name variable defined in this scope.........if both are same scope.

    ReplyDelete
  11. One more difference i would like to add. List is inherited from IList interface as well hence we can have random access using index where as IEnumerable implementaion does not allow such random access.

    ReplyDelete
  12. What are extension methods?

    ReplyDelete
    Replies
    1. Adding some extra functionality to the existing method with out changing its original functionality is called as extension method.

      Delete
  13. how we can handle exception if occur in finally block

    ReplyDelete
  14. Why ienumerable doesn't allow random access and what meant by random access in a list..

    ReplyDelete
  15. My question is about method overloading, it was asked with different datatype

    int add(int a, int b)
    double add(int a, int b)

    will it be method overloading

    ReplyDelete

If you are aware of any other C# questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us