Thursday, August 14, 2014
Generic Collection
When to Use Generic Collections
.NET Framework 4.5 Other Versions .NET Framework 4 .NET Framework 3.0 Silverlight .NET Framework 3.5 .NET Framework 2.0 4 out of 5 rated this helpful - Rate this topic
Using generic collections is generally recommended, because you gain the immediate benefit of type safety without having to derive from a base collection type and implement type-specific members. Generic collection types also generally perform better than the corresponding nongeneric collection types (and better than types that are derived from nongeneric base collection types) when the collection elements are value types, because with generics there is no need to box the elements.
For programs that target the .NET Framework 4 or later, you should use the generic collection classes in the System.Collections.Concurrent namespace when multiple threads might be adding or removing items from the collection concurrently.
The following generic types correspond to existing collection types:
•List is the generic class that corresponds to ArrayList.
•Dictionary and ConcurrentDictionary are the generic classes that correspond to Hashtable.
•Collection is the generic class that corresponds to CollectionBase. Collection can be used as a base class, but unlike CollectionBase, it is not abstract. This makes it much easier to use.
•ReadOnlyCollection is the generic class that corresponds to ReadOnlyCollectionBase. ReadOnlyCollection is not abstract, and has a constructor that makes it easy to expose an existing List as a read-only collection.
•The Queue, ConcurrentQueue, Stack, ConcurrentStack, and SortedList generic classes correspond to the respective nongeneric classes with the same names.
Additional Types
--------------------------------------------------------------------------------
Several generic collection types do not have nongeneric counterparts. They include the following:
•LinkedList is a general-purpose linked list that provides O(1) insertion and removal operations.
•SortedDictionary is a sorted dictionary with O(log n) insertion and retrieval operations, which makes it a useful alternative to SortedList.
•KeyedCollection is a hybrid between a list and a dictionary, which provides a way to store objects that contain their own keys.
•BlockingCollection implements a collection class with bounding and blocking functionality.
•ConcurrentBag provides fast insertion and removal of unordered elements.
LINQ to Objects
--------------------------------------------------------------------------------
The LINQ to Objects feature enables you to use LINQ queries to access in-memory objects as long as the object type implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable interface. LINQ queries provide a common pattern for accessing data; are typically more concise and readable than standard foreach loops; and provide filtering, ordering and grouping capabilities. LINQ queries can also improve performance. For more information, see LINQ to Objects and Parallel LINQ (PLINQ).
Additional Functionality
--------------------------------------------------------------------------------
Some of the generic types have functionality that is not found in the nongeneric collection types. For example, the List class, which corresponds to the nongeneric ArrayList class, has a number of methods that accept generic delegates, such as the Predicate delegate that allows you to specify methods for searching the list, the Action delegate that represents methods that act on each element of the list, and the Converter delegate that lets you define conversions between types.
The List class allows you to specify your own IComparer generic interface implementations for sorting and searching the list. The SortedDictionary and SortedList classes also have this capability. In addition, these classes let you specify comparers when the collection is created. In similar fashion, the Dictionary and KeyedCollection classes let you specify your own equality comparers.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment