While I do a program, sometimes I've got this doubt. I have been using List<T>
but I haven't used the others.
I'd like to know when each one is better to use, and under what circumstances.
I'm sure you can read the documentation by yourself. I'll give a short summary here:
IEnumerable
is an interface that exposes an enumerator over a collection. Implement this interface if want to be able to support iteration, for example inside a foreach
loop. Collection
is an implementation of IEnumerable
(therefore you can iterate over it) that is generally further extended by user-defined classes that want to have collection-like behavior (ie use Add
, Remove
, Contains
, etc.). It can also be used "as-is". List
is also an implementation of IEnumerable
(therefore you can iterate over it) that is generally used "as-is" as a container for objects of a certain type. Uses a dynamically adjusting array in the background and is the generic equivalent of ArrayList
. Note that while IEnumerable
can be both generic and non-generic, Collection
and List
can only be used as generic classes.
MSDN可以:
List is instantiable type that holds items in a linear fashion. (Edit: I'm told it's implemented with a dynamic array, rather than with a Linked List, which was just my guess).
ICollection
/ CollectionBase
and IEnumerable
aren't directly instantiable types.
CollectionBase
/ ICollection
is a base class/interface that is inherited/implemented by any class that considers itself a collection and holds multiple items. CollectionBase
/ ICollection
also allows you to pass around collections without knowing their implementation.
IEnumerable
is also a interface that provides methods for iterating over the collection. IEnumerable
basically lets you use foreach
loops on the collection. You can use the interface methods directly to get some C++ style iterators going, but the foreach
loop is less error prone.
If you look at the definitions of the three (see below) you will notice that List
implements Enumerable
and ICollection
and ICollection
implements IEnumerable
. More clearly:
List
is CLASS which can store a variable number of items of the same type plus the functionality specified by the other two interfaces.
ICollection
is an interface which specifies a generic way to manipulate collections. NOTE: this is an interface and thus is NOT capable of being instantiated.
IEnumerable
is an interface which specifies a means of iterating over a collection. NOTE: this is an interface and thus is NOT capable of being instantiated.
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.
public class List<T> : IList<T>, ICollection<T>,
IEnumerable<T>, IList, ICollection, IEnumerable
Defines methods to manipulate generic collections.
public interface ICollection<T> : IEnumerable<T>,
IEnumerable
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
public interface IEnumerable<out T> : IEnumerable
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.