-1
    private static void Main(string[] args)
    {
        var query = Data();
    }

    private static IList<Employee> Data()
    {
        List<Employee> list = new List<Employee>();
        list.Add(new Employee { Name = "test0", Age = 19 });
        list.Add(new Employee { Name = "test1", Age = 33 });
        list.Add(new Employee { Name = "test2", Age = 25 });
        return list;
    }

    private static IEnumerable<Employee> Data()
    {
        List<Employee> list = new List<Employee>();
        list.Add(new Employee { Name = "test0", Age = 19 });
        list.Add(new Employee { Name = "test1", Age = 33 });
        list.Add(new Employee { Name = "test2", Age = 25 });
        return list;
    }

When I run code above, I didn't see any different result when result type in IEnumerable or IList. Is there any different using both of them? How to decide which type to use?

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
nettium
  • 67
  • 2
  • 3
  • 9
  • Generally speaking it's best to use as an input the most general type that you can: So, accepting an `IEnumerable` is a parameter is better than accepting a `List`. Also, an `IEnumerable` does not allow the callee to change the collection, which is also good to know from the point of view of the caller. – Matthew Watson Feb 18 '17 at 09:20
  • Welcome to Stack Overflow. Before asking questions like this you should do some research by searching already asked questions to see if there is already an answer to your question. Check the [Help Centre](https://stackoverflow.com/help/asking) about asking questions – Nkosi Feb 18 '17 at 09:23
  • have a look at this [link](http://www.c-sharpcorner.com/UploadFile/78607b/difference-between-ienumerable-icollection-and-ilist-interf/). This has a very clear explaination for this question – Prashanth Benny Feb 18 '17 at 09:24
  • Generally speaking, in private methods it's best to use as return type the most specific type that you can, in this case `List`, for performance reasons. For example, all of them allow you to `foreach` over the result, but when returning a `List`, that `foreach` will be most efficient. You will also be able to use all the features of the list without having to cast. And even if it doesn't help, it doesn't hurt either, it's a `private` method anyway. – Kris Vandermotten Feb 18 '17 at 10:57

1 Answers1

2

IList inherits from IEnumerable so they are essentially the same just IList has extra functionality.

It depends on what you are doing with that data once its created as to whether you should use an IEnumerable or IList.

IEnumerable is good if you only want to iterate over the collections content as its readonly.

IList allows adding and removing of content (so does ICollection) and allows direct access to elements with an index

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Oliver Cooke
  • 1,059
  • 1
  • 8
  • 22
  • Hi, can give some sample that shows differences using IEnumerable and IList? TQ. – nettium Feb 18 '17 at 09:19
  • @nettium Hope that provides more clarity. So you would not be able to use the add, remove or reference via index if you use IEnummerable you would if you used list. So its dependent on what you are doing with that collection after it is created. – Oliver Cooke Feb 18 '17 at 09:26