1) The generic collections were introduced in .NET 2.0 in the System.Collections.Generic 2_ namespace.
2) The generic collections are unsynchronized
3) concurrent read access (all writes done at beginning and never again) is always safe, but for concurrent mixed access you should either synchronize the collection or use one of the concurrent collections.
There are two kinds of generic collections
1) Associative : Associative collections store a value in the collection by providing a key that is used to add/remove/lookup the item
2) Non - Associative : They don't use keys to manipulate the collection but rely on the object itself being stored or some other means (such as index) to manipulate the collection
Associative Classes
Dictionary<TKey,TVale>
The Dictionary<TKey,TVale> is probably the most used associative container class. The Dictionary<TKey,TValue> is the fastest class for associative lookups/inserts/deletes because it uses a hash table under the covers. The insert/delete/lookup time of items in the dictionary is amortized constant time - O(1) - which means no matter how big the dictionary gets, the time it takes to find something remains relatively constant
The SortedDictionary<TKey,TValue> is similar to the Dictionary<TKey,TValue> in usage but very different in implementation. The SortedDictionary<TKey,TValye> uses a binary tree under the covers to maintain the items in order by the key. As a consequence of sorting.
Non - Associative Classes
The List<T> is a basic contiguous storage container. Some people may call this a vector or dynamic array. Essentially it is an array of items that grow once its current capacity is exceeded. Because the items are stored contiguously as an array, you can access items in the List<T> by index very quickly. However inserting and removing in the beginning or middle of the List<T> are very costly because you must shift all the items up or down as you delete or insert respectively
There are several other Non - Associative generic collections like LinkedList<T>, Stack<T>, Queue<T>
The older non-generic collections like ArrayList, are non-type-safe and in some cases less performant than their generic counterparts.
Concurrent Classes.
The concurrent collections are new as of .NET 4.0 and are included in the System.Collections.Concurrent namespace. These collections are optimized for use in situations where multi-threaded read and write access of a collection is desired.
List<T> Examples
2) The generic collections are unsynchronized
3) concurrent read access (all writes done at beginning and never again) is always safe, but for concurrent mixed access you should either synchronize the collection or use one of the concurrent collections.
There are two kinds of generic collections
1) Associative : Associative collections store a value in the collection by providing a key that is used to add/remove/lookup the item
2) Non - Associative : They don't use keys to manipulate the collection but rely on the object itself being stored or some other means (such as index) to manipulate the collection
Associative Classes
Dictionary<TKey,TVale>
The Dictionary<TKey,TVale> is probably the most used associative container class. The Dictionary<TKey,TValue> is the fastest class for associative lookups/inserts/deletes because it uses a hash table under the covers. The insert/delete/lookup time of items in the dictionary is amortized constant time - O(1) - which means no matter how big the dictionary gets, the time it takes to find something remains relatively constant
The SortedDictionary<TKey,TValue> is similar to the Dictionary<TKey,TValue> in usage but very different in implementation. The SortedDictionary<TKey,TValye> uses a binary tree under the covers to maintain the items in order by the key. As a consequence of sorting.
Non - Associative Classes
The List<T> is a basic contiguous storage container. Some people may call this a vector or dynamic array. Essentially it is an array of items that grow once its current capacity is exceeded. Because the items are stored contiguously as an array, you can access items in the List<T> by index very quickly. However inserting and removing in the beginning or middle of the List<T> are very costly because you must shift all the items up or down as you delete or insert respectively
There are several other Non - Associative generic collections like LinkedList<T>, Stack<T>, Queue<T>
The older non-generic collections like ArrayList, are non-type-safe and in some cases less performant than their generic counterparts.
Concurrent Classes.
The concurrent collections are new as of .NET 4.0 and are included in the System.Collections.Concurrent namespace. These collections are optimized for use in situations where multi-threaded read and write access of a collection is desired.
- ConcurrentQueue
- Thread-safe version of a queue (FIFO).
- ConcurrentStack
- Thread-safe version of a stack (LIFO)
- ConcurrentBag
- Thread-safe unordered collection of objects.
- Optimized for situations where a thread may be bother reader and writer.
- BlockingCollection
- ConcurrentDictionary
- Thread-safe version of a dictionary.
- Optimized for multiple readers (allows multiple readers under same lock).
- BlockingCollection
- Wrapper collection that implement producers & consumers paradigm.
- Readers can block until items are available to read.
- Writers can block until space is available to write (if bounded).
List<T> Examples
List<int> list = new List<int>(); //Add
list.Add(2);
list.Add(3);
list.Add(5);
list.Add(7);
list.Remove(5); //Delete
List<int> list = new List<int>(new int[] { 19, 23, 29 });//Find // Finds first element greater than 20 int result = list.Find(item => item > 20); Console.WriteLine(result);DictionaryDictionary<string, int> dictionary = new Dictionary<string, int>(); dictionary.Add("cat", 2); dictionary.Add("dog", 1); dictionary.Add("llama", 0); dictionary.Add("iguana", -1);KeyValuePair KeyValuePair: Key and Value properties When Dictionary, or any object that implements IDictionary,is used in a foreach-loop, it returns an enumeration.foreach (KeyValuePair<string, int> pair in dictionary ) { Console.WriteLine("{0}, {1}", pair.Key, pair.Value); }Iterating using KeyValuePair is faster than using .keys or ,values.List Vs Dictionary1) it is faster to loop through all the elements in a List than in a Dictionary.2) For lookups dictionary is fasterArrays vs DictionaryArrays are faster in terms of lookups compared to Dictionary