This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
IList<int> vs List<int>
C# - List<T> or IList<T>
What is the difference between List and IList, which one has better performance and when to use List over IList and vice versa?
In C# List is a concrete implementation of the IList interface. The List is an implementation of the IList Interface. The idea is to program against the interface, not the implementation. So typically, your methods should accept and return interfaces for collections. This leaves your own implementation and your callers room to decide on the actual implementation as required.
Benefit of using an Interface is that you get to implement your functionality or better yet, the only functionality you require. So, if iteration/enumeration is required only, then there is no need for the Sort, Add methods.
List implements IList interface
IList is a interface and doesn't have any implementation, so the performance of IList depending the class it implements
IList is the interface - see this question for more information - List<T> or IList<T>
Related
This question already has an answer here:
implement ienumerable with ienumerable<T>
(1 answer)
Closed 3 years ago.
If I just want to use the generic version IEnumerable, I have to implement not only IEnumerator<T> GetEnumerator() but also IEnumerator GetEnumerator(), the latter seems to be redundant
IEnumerable interface was introduced in 1.1 .net framework.
But generic version IEnumerable<T> in 2.0 only (with generics). So, it's backward compatibility. There is legacy code, that accept only IEnumerable:
void Method(IEnumerable list)
{
foreach(var item in list)
{
Console.WriteLine(item);
}
}
So if IEnumerable<T> does not implement IEnumerable, you can not pass List<int> in this method, for example.
Basically, there are two reasons:
compatibility. A lot of legacy code, which can deal with IEnumerable, but can't deal with IEnumerable<T>. Yes, modern APIs are being designed to use generics, but from time to time we need to use legacy APIs.
there are cases, when you want to enumerate, but don't care at design-time about object type or boxing. IEnumerable is OK here. Data binding is the most obvious one.
This question already has answers here:
List<T> or IList<T> [closed]
(18 answers)
Closed 8 years ago.
Why here interface Ilist is used to create an object of List type
IList<BankAccountView> bankAccountViews = new List<BankAccountView>();
when it can be done like this
List<BankAccountView> bankAccountViews = new List<BankAccountView>();
A List is a concrete type, while an IList is a contract for which you can use any implemtation that an IList has.
An IList has a set of methods as defined on MSDN: http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx
Here's a very good blogpost explaining it in detail: http://www.claudiobernasconi.ch/2013/07/22/when-to-use-ienumerable-icollection-ilist-and-list/
So when writing a function or method that takes a collection, write it not to take a List, but an IList, an ICollection, or IEnumerable. 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 is really all you should be asking for.
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 internally, return a copy as a List.
Read link: When to use IList and when to use List
This question already has answers here:
When to use IList and when to use List
(12 answers)
Closed 9 years ago.
I saw a function like
public FuncA(string param1, IList<SqlParameter> sqlParamsList)
I wonder why the author was using IList instead of List? Which one is better? I know the difference between them is one is interface, the other one is class. So my final question is when to use which?
More than one class can implement the IList interface. If you use IList you accept any class that implements IList.
If you use List, you can only pass List and derived classes.
Using an interface is generally preferable as it makes the API more flexible for the caller. If your method accepts the interface rather than the concrete implementation, the caller can use whatever type they want (List, Array, ImmutableList...), as long as it implements IList.
Using an interface is preferred over a concrete type in order to allow a caller to pass in any object that implements the interface.
Especially in public methods this is good practice.
I would tend towards using the IList interface parameter over the concrete implementation, unless there was some reason you absolutely HAD to have the concrete List parameter.
By using IList instead of List, your method can now accept all collection types that implement IList, which may or may not be a collection that directly inherits from List. List and any of its subclasses implement IList as well, so they would also be included in the set of available types your method could use. Using IList in this case allows you to be more flexible.
Also, in unit testing scenarios, IList may be easier to mock out, depending on what exactly you're trying to test and what your features your mocking framework has.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is there not a ForEach extension method on the IEnumerable interface?
Why ForEach extension method is not available in ObservableCollections class while it is available in List ?
As JJohn says, it's by design. Read this post by Eric Lippert for more information. A standard foreach loop works just as well and is generally more readable.
ForEach is available only in List<T> and not in any other collection. It is just design decision.
Foreach can be used only on those objects which are of classes that implements IEnumerable.
IEnumerable is an interface that defines one method GetEnumerator which returns an IEnumerator interface, this in turn allows readonly access to a collection. A collection that implements IEnumerable can be used with a foreach statement.
This question already has answers here:
IList vs IEnumerable for Collections on Entities
(2 answers)
Closed 9 years ago.
Can anyone tell me when I should use either.
For example, I think I should use an IList when I want to access the .Count of the collection or an individual item, correct?
Thank you.
Generally speaking, you should try and use the least specific type that suits your purpose. IEnumerable is less specific than IList (IList implements IEnumerable) so unless you want something specific from IList (such as Count as you suggest, or perhaps Add, Delete, etc), I'd use IEnumerable.
One benefit of remaining with IEnumerable is that you can write iterator methods to return this type (look up "yield return" and iterator methods if you are not familiar with them). This allows you to write very memory efficient "pipelines" for your loops.
You use IEnumerable when you want to loop through the items in a collection.
IList is when you want to add, remove, and access the list contents out of order.
IList vs IEnumerable for Collections on Entities
http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=722
You should use IList when you need access by index to your collection, add and delete elements, etc., and IEnumerable when you need just enumerate over your collection.
A very simple answer, I can extend it if you will describe you scenario.