<p>I know that IList is the interface and List is the concrete type but I still don't know when to use each one. What I'm doing now is if I don't need the Sort or FindAll methods I use the interface. Am I right? Is there a better way to decide when to use the interface or the concrete type?</p>

[{'answer_id': 17177, 'author': 'swilliams', 'author_id': 736, 'author_profile': 'https://Stackoverflow.com/users/736', 'pm_score': 2, 'selected': False, 'text': "<p>I don't think there are hard and fast rules for this type of thing, but I usually go by the guideline of using the lightest possible way until absolutely necessary.</p>\n\n<p>For example, let's say you have a <code>Person</code> class and a <code>Group</code> class. A <code>Group</code> instance has many people, so a List here would make sense. When I declare the list object in <code>Group</code> I will use an <code>IList&lt;Person&gt;</code> and instantiate it as a <code>List</code>.</p>\n\n<pre><code>public class Group {\n  private IList&lt;Person&gt; people;\n\n  public Group() {\n    this.people = new List&lt;Person&gt;();\n  }\n}\n</code></pre>\n\n<p>And, if you don't even need everything in <code>IList</code> you can always use <code>IEnumerable</code> too. With modern compilers and processors, I don't think there is really any speed difference, so this is more just a matter of style.</p>\n"}, {'answer_id': 17179, 'author': 'Dan Herbert', 'author_id': 392, 'author_profile': 'https://Stackoverflow.com/users/392', 'pm_score': 1, 'selected': False, 'text': "<p>In situations I usually come across, I rarely use IList directly.</p>\n\n<p>Usually I just use it as an argument to a method  </p>\n\n<pre><code>void ProcessArrayData(IList almostAnyTypeOfArray)\n{\n    // Do some stuff with the IList array\n}\n</code></pre>\n\n<p>This will allow me to do generic processing on almost any array in the .NET framework, unless it uses IEnumerable and not IList, which happens sometimes.</p>\n\n<p>It really comes down to the kind of functionality you need. I'd suggest using the List class in most cases. IList is best for when you need to make a custom array that could have some very specific rules that you'd like to encapsulate within a collection so you don't repeat yourself, but still want .NET to recognize it as a list.</p>\n"}, {'answer_id': 17183, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 3, 'selected': False, 'text': "<p>If you're working within a single method (or even in a single class or assembly in some cases) and no one outside is going to see what you're doing, use the fullness of a List. But if you're interacting with outside code, like when you're returning a list from a method, then you only want to declare the interface without necessarily tying yourself to a specific implementation, especially if you have no control over who compiles against your code afterward. If you started with a concrete type and you decided to change to another one, even if it uses the same interface, you're going to break someone else's code unless you started off with an interface or abstract base type. </p>\n"}, {'answer_id': 17185, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 2, 'selected': False, 'text': '<p>You should use the interface only if you need it, e.g., if your list is casted to an IList implementation other than List. This is true when, for example, you use NHibernate, which casts ILists into an NHibernate bag object when retrieving data.</p>\n\n<p>If List is the only implementation that you will ever use for a certain collection, feel free to declare it as a concrete List implementation.</p>\n'}, {'answer_id': 17216, 'author': 'tgmdbm', 'author_id': 1851, 'author_profile': 'https://Stackoverflow.com/users/1851', 'pm_score': 3, 'selected': False, 'text': "<p>It's always best to use the lowest base type possible. This gives the implementer of your interface, or consumer of your method, the opportunity to use whatever they like behind the scenes.</p>\n\n<p>For collections you should aim to use IEnumerable where possible. This gives the most flexibility but is not always suited.</p>\n"}, {'answer_id': 17295, 'author': 'Lee', 'author_id': 1954, 'author_profile': 'https://Stackoverflow.com/users/1954', 'pm_score': 9, 'selected': True, 'text': "<p>There are two rules I follow:</p>\n\n<ul>\n<li>Accept the most basic type that will work</li>\n<li>Return the richest type your user will need</li>\n</ul>\n\n<p>So when writing a function or method that takes a collection, write it not to take a List, but an IList&lt;T&gt;, an ICollection&lt;T&gt;, or IEnumerable&lt;T&gt;.  The generic interfaces will still work even for heterogenous lists because System.Object can be a T too.  Doing this will save you headache if you decide to use a Stack or some other data structure further down the road.  If all you need to do in the function is foreach through it, IEnumerable&lt;T&gt; is really all you should be asking for.</p>\n\n<p>On the other hand, when returning an object out of a function, you want to give the user the richest possible set of operations without them having to cast around.  So in that case, if it's a List&lt;T&gt; internally, return a copy as a List&lt;T&gt;.</p>\n"}, {'answer_id': 17638, 'author': 'ICR', 'author_id': 214, 'author_profile': 'https://Stackoverflow.com/users/214', 'pm_score': 5, 'selected': False, 'text': "<p>I would agree with Lee's advice for taking parameters, but not returning.</p>\n\n<p>If you specify your methods to return an interface that means you are free to change the exact implementation later on without the consuming method ever knowing. I thought I'd never need to change from a List&lt;T> but had to later change to use a custom list library for the extra functionality it provided. Because I'd only returned an IList&lt;T> none of the people that used the library had to change their code.</p>\n\n<p>Of course that only need apply to methods that are externally visible (i.e. public methods). I personally use interfaces even in internal code, but as you are able to change all the code yourself if you make breaking changes it's not strictly necessary.</p>\n"}, {'answer_id': 85441, 'author': 'Joe', 'author_id': 13087, 'author_profile': 'https://Stackoverflow.com/users/13087', 'pm_score': 6, 'selected': False, 'text': '<p>Microsoft guidelines as checked by FxCop discourage use of List&lt;T&gt; in public APIs - prefer IList&lt;T&gt;.</p>\n\n<p>Incidentally, I now almost always declare one-dimensional arrays as IList&lt;T&gt;, which means I can consistently use the IList&lt;T&gt;.Count property rather than Array.Length.  For example:</p>\n\n<pre><code>public interface IMyApi\n{\n    IList&lt;int&gt; GetReadOnlyValues();\n}\n\npublic class MyApiImplementation : IMyApi\n{\n    public IList&lt;int&gt; GetReadOnlyValues()\n    {\n        List&lt;int&gt; myList = new List&lt;int&gt;();\n        ... populate list\n        return myList.AsReadOnly();\n    }\n}\npublic class MyMockApiImplementationForUnitTests : IMyApi\n{\n    public IList&lt;int&gt; GetReadOnlyValues()\n    {\n        IList&lt;int&gt; testValues = new int[] { 1, 2, 3 };\n        return testValues;\n    }\n}\n</code></pre>\n'}, {'answer_id': 85491, 'author': 'petr k.', 'author_id': 15497, 'author_profile': 'https://Stackoverflow.com/users/15497', 'pm_score': 2, 'selected': False, 'text': '<p>You are most often better of using the most general usable type, in this case the IList or even better the IEnumerable interface, so that you can switch the implementation conveniently at a later time.</p>\n\n<p>However, in .NET 2.0, there is an annoying thing - IList does not have a <strong>Sort()</strong> method. You can use a supplied adapter instead:</p>\n\n<pre><code>ArrayList.Adapter(list).Sort()\n</code></pre>\n'}, {'answer_id': 17761596, 'author': 'rajesh', 'author_id': 2602088, 'author_profile': 'https://Stackoverflow.com/users/2602088', 'pm_score': 5, 'selected': False, 'text': '<p><strong>IEnumerable</strong><br>\nYou should try and use the least specific type that suits your purpose.<br>\n<code>IEnumerable</code> is less specific than <code>IList</code>.<br>\nYou use <code>IEnumerable</code> when you want to loop through the items in a collection.  </p>\n\n<p><strong>IList</strong><br>\n<code>IList</code> implements <code>IEnumerable</code>.<br>\nYou should use <code>IList</code> when you need access by index to your collection, add and delete elements, etc...</p>\n\n<p><strong>List</strong><br>\n<code>List</code> implements <code>IList</code>.</p>\n'}, {'answer_id': 18306587, 'author': 'Javid', 'author_id': 1701894, 'author_profile': 'https://Stackoverflow.com/users/1701894', 'pm_score': 1, 'selected': False, 'text': "<p>A <code>List</code> object allows you to create a list, add things to it, remove it, update it, index into it and etc. <code>List</code> is used whenever you just want a generic list where you specify object type in it and that's it.</p>\n<p><code>IList</code> on the other hand is an Interface. Basically, if you want to create your own custom <code>List</code>, say a list class called <code>BookList</code>, then you can use the Interface to give you basic methods and structure to your new class. <code>IList</code> is for when you want to create your own, special sub-class that implements List.</p>\n<p>Another difference is:\n<code>IList</code> is an Interface and cannot be instantiated. <code>List</code> is a class and can be instantiated. It means:</p>\n<pre><code>IList&lt;string&gt; list1 = new IList&lt;string&gt;(); // this is wrong, and won't compile\n\nIList&lt;string&gt; list2 = new List&lt;string&gt;();  // this will compile\nList&lt;string&gt; list3 = new List&lt;string&gt;();   // this will compile\n</code></pre>\n"}, {'answer_id': 19684049, 'author': 'Matthew Watson', 'author_id': 106159, 'author_profile': 'https://Stackoverflow.com/users/106159', 'pm_score': 5, 'selected': False, 'text': "<p>There's an important thing that people always seem to overlook:</p>\n\n<p>You can pass a plain array to something which accepts an <code>IList&lt;T&gt;</code> parameter, and then you can call <code>IList.Add()</code> and will receive a runtime exception:</p>\n\n<p><code>Unhandled Exception: System.NotSupportedException: Collection was of a fixed size.</code></p>\n\n<p>For example, consider the following code:</p>\n\n<pre><code>private void test(IList&lt;int&gt; list)\n{\n    list.Add(1);\n}\n</code></pre>\n\n<p>If you call that as follows, you will get a runtime exception:</p>\n\n<pre><code>int[] array = new int[0];\ntest(array);\n</code></pre>\n\n<p>This happens because using plain arrays with <code>IList&lt;T&gt;</code> violates the Liskov substitution principle.</p>\n\n<p>For this reason, if you are calling <code>IList&lt;T&gt;.Add()</code> you may want to consider requiring a <code>List&lt;T&gt;</code> instead of an <code>IList&lt;T&gt;</code>.</p>\n"}]
['https://Stackoverflow.com/questions/17170', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1560/']