Collections
Many collection types, except List implement IEnumerable therefore you can pass different collection types to the same function. As you can see from the diagram above of how the classes and Interface inheritance the class IEnumerable. The colored ones are the class files and the white ones are the Interface.
IEnumerable<T> and IEnumerable
Provides minimum functionality (enumeration only)
ICollection<T> and ICollection
Provides medium functionality (e.g., the Count property)
IList<T>/Idictionary <K,V>
Provide maximum functionality (including random access by index/key)
- Iterates over the elements of the collection from beginning to the end.
- It allows minimum exposure of properties or parameter requirements.
- Foreach loop is the way to iterate over the elements
There are basically two types of collections:
1. Non-Generic Collections
Non-generic collection are not strongly typed and store data as object. To retrieve the value type from non-generic collection, it needs to be boxed first and then unboxed which makes processing very slow, for reference types you will need to cast them also.
2. Generic Collections
Generic collection are strongly typed and store elements internally in array of their actual type.
Namespace | Contains |
---|---|
System.Collections | Nongeneric collection classes and interfaces |
System.Collections.Specialized | Strongly typed nongeneric collection classes |
System.Collections.Generic | Generic collection classes and interfaces |
System.Collections.ObjectModel | Proxies and bases for custom collections |
System.Collections.Concurrent | Thread-safe collections |