{"id":1337,"date":"2014-02-13T21:10:27","date_gmt":"2014-02-13T21:10:27","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1337"},"modified":"2014-02-13T21:10:27","modified_gmt":"2014-02-13T21:10:27","slug":"net-collections-refresher","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/net-collections-refresher\/","title":{"rendered":".NET collections refresher"},"content":{"rendered":"<p>I&#8217;ve been writing C#\/.NET code for a long time, but every now and then I feel the need to give myself a refresher on the basics and it usually results in finding features that have been added which I knew nothing about. You know how you get used to using classes so look no further. Well here&#8217;s my refresher on the .NET collection classes as of .NET 4.5<\/p>\n<p><strong>ArrayList<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.arraylist(v=vs.110).aspx\" title=\"ArrayList Class\" target=\"_blank\">ArrayList <\/a>is a wrapper around a standard object[]. It&#8217;s weakly typed and may suffer in performance tests against a generic array type such as a List&lt;T&gt; due to boxing\/unboxing.<\/p>\n<p>Unlike a standard object[] declared arrays, multidimensional arrays elements are not directly supported. Although ofcourse you can create an ArrayList or ArrayLists.<\/p>\n<p><strong>BitArray<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.bitarray(v=vs.110).aspx\" title=\"BitArray Class\" target=\"_blank\">BitArray<\/a> class is a specialized collection for handling an array of bits. It allows us to manipulate the bits in various ways, but lacks bit shifting (for example).<\/p>\n<p><strong>BlockingCollection&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd267312(v=vs.110).aspx\" title=\"BlockingCollection Class\" target=\"_blank\">BlockingCollection<\/a> class wraps an IProducerConsumerCollection&lt;T&gt; to allow thread-safe adding and removing of items. It also offers bounding capabilities.<\/p>\n<p><strong>ConcurrentBag&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd381779(v=vs.110).aspx\" title=\"ConcurrentBag Class\" target=\"_blank\">ConcurrentBag<\/a> is a thread-safe bag. Bags are used to store objects with no concern for ordering. They support duplicates and nulls.<\/p>\n<p><strong>ConcurrentDictionary&lt;TKey, TValue&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd287191(v=vs.110).aspx\" title=\"ConcurrentDictionary Class\" target=\"_blank\">ConcurrentDictionary<\/a> is a thread-safe dictionary.<\/p>\n<p><strong>ConcurrentQueue&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd267265(v=vs.110).aspx\" title=\"ConcurrentQueue Class\" target=\"_blank\">ConcurrentQueue<\/a> is a thread-safe Queue (FIFO collection).<\/p>\n<p><strong>ConcurrentStack&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd267331(v=vs.110).aspx\" title=\"ConcurrentStack Class\" target=\"_blank\">ConcurrentStack<\/a> is a thread-safe Queue (LIFO collection).<\/p>\n<p><strong>Collection&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms132397(v=vs.110).aspx\" title=\"Collection Class\" target=\"_blank\">Collection<\/a> class is a base class for generic collections. Whilst you can instantiate a Collection&lt;T&gt; directly (i.e. it&#8217;s not abstract) it&#8217;s designed more from the point of view of extensibility, i.e. deriving you own collection type from it. It only really differs from a List&lt;T&gt;, in that it offersvirtual methods for you to overload to customize your collection, whereas a List&lt;T&gt; doesn&#8217;t offer such extensibility.<\/p>\n<p><strong>Dictionary&lt;TKey, TValue&gt;<\/strong><\/p>\n<p>A key\/value generic class. Duplicates are not allowed as keys, nor are nulls. For a thread-safe implementation, look at ConcurrentDictionary&lt;TKey, TValue&gt;.<\/p>\n<p><strong>HashSet&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb359438(v=vs.110).aspx\" title=\"HashSet&lt;T&gt; Class\" target=\"_blank\">HashSet&lt;T&gt;<\/a> is a set collection and thus cannot contain duplicate elements. Elements are not held in any particular order. It is aimed a set type operations, such as IntersectWith, Overlap etc.<\/p>\n<p><strong>Hashtable<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.hashtable(v=vs.110).aspx\" title=\"Hashtable Class\" target=\"_blank\">Hashtable<\/a> stores key\/value pairs which are organised based upon the hash code of the key. As per the Dictionary class, keys cannot be null and duplicates are not allowed. Basically it&#8217;s a weakly typed Dictionary.<\/p>\n<p><strong>HybridDictionary<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.specialized.hybriddictionary(v=vs.110).aspx\" title=\"HybridDictionary Class\" target=\"_blank\">HybridDictionary<\/a> is a weakly typed Dictionary which can switch between using the ListDictionary for when there&#8217;s only a few items stored and then switching to the Hashtable when the collection gets large.<\/p>\n<p><strong>ImmutableDictionary&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dn467181(v=vs.110).aspx\" title=\"ImmutableDictionary Class\" target=\"_blank\">ImmutableDictionary<\/a> is, simply put, an immutable Dictionary.<\/p>\n<p>See also <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.immutable.immutableinterlocked(v=vs.110).aspx\" title=\"ImmutableInterlocked\" target=\"_blank\">ImmutableInterlocked <\/a>.<\/p>\n<p><strong>ImmutableHashSet&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dn467171(v=vs.110).aspx\" title=\"ImmutableHashSet Class\" target=\"_blank\">ImmutableHashSet<\/a> is, simply put, an immutable HashSet.<\/p>\n<p>See also <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.immutable.immutableinterlocked(v=vs.110).aspx\" title=\"ImmutableInterlocked\" target=\"_blank\">ImmutableInterlocked <\/a>. <\/p>\n<p><strong>ImmutableList&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dn467185(v=vs.110).aspx\" title=\"ImmutableList Class\" target=\"_blank\">ImmutableList<\/a> is, simply put, an immutable List.<\/p>\n<p>See also <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.immutable.immutableinterlocked(v=vs.110).aspx\" title=\"ImmutableInterlocked\" target=\"_blank\">ImmutableInterlocked <\/a>.<\/p>\n<p><strong>ImmutableQueue&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dn467186(v=vs.110).aspx\" title=\"ImmutableQueue Class\" target=\"_blank\">ImmutableQueue<\/a> is, simply put, an immutable Queue.<\/p>\n<p>See also <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.immutable.immutableinterlocked(v=vs.110).aspx\" title=\"ImmutableInterlocked\" target=\"_blank\">ImmutableInterlocked <\/a>. <\/p>\n<p><strong>ImmutableSortedDictionary&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dn467194(v=vs.110).aspx\" title=\"ImmutableSortedDictionary Class\" target=\"_blank\">ImmutableSortedDictionary<\/a> is, simply put, an immutable SortedDictionary.<\/p>\n<p>See also <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.immutable.immutableinterlocked(v=vs.110).aspx\" title=\"ImmutableInterlocked\" target=\"_blank\">ImmutableInterlocked <\/a>. <\/p>\n<p><strong>ImmutableSortedSet&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dn467193(v=vs.110).aspx\" title=\"ImmutableSortedSet Class\" target=\"_blank\">ImmutableSortedSet<\/a> is, simply put, an immutable SortedSet.<\/p>\n<p>See also <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.immutable.immutableinterlocked(v=vs.110).aspx\" title=\"ImmutableInterlocked\" target=\"_blank\">ImmutableInterlocked <\/a>. <\/p>\n<p><strong>ImmutableStack&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dn467197(v=vs.110).aspx\" title=\"ImmutableStack\" target=\"_blank\">ImmutableStack<\/a> is, simply put, an immutable Stack.<\/p>\n<p>See also <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.immutable.immutableinterlocked(v=vs.110).aspx\" title=\"ImmutableInterlocked\" target=\"_blank\">ImmutableInterlocked <\/a>. <\/p>\n<p><strong>KeyedCollection&lt;TKey, TItem&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms132438(v=vs.110).aspx\" title=\"KeyedCollection Class\" target=\"_blank\">KeyedCollection<\/a> is an abstract class which is a hybrid between a collection based upon an IList&lt;T&gt; and an IDictionary&lt;TKey, TItem&gt;. Unlike a Dictionary, the element stored is not a key\/value pair. It&#8217;s key is embedded in it&#8217;s value.<\/p>\n<p><strong>LinkedList&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/he2s3bh7(v=vs.110).aspx\" title=\"LinkedList Class\" target=\"_blank\">LinkedList<\/a> class is really that, a doubly linked list. Obviously this means insertion and removal of items are O(1) operations. Copying of elements requires far less memory than copying of array like structures, and obviously when adding more an more items the underlying data structure itself does not need to resize as an array would need to. On the downside each element requires a next and previous reference along with the data itself plus you do not get O(1) access to elements and this in itself may be a major downside &#8211; depending upon your app. <\/p>\n<p><strong>List&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6sh2ey19(v=vs.110).aspx\" title=\"List Class\" target=\"_blank\">List<\/a> is in essence this is a generic ArrayList but type safe and due to the use of generics is more efficient in performance terms when handling value types which require boxing\/unboxing.<\/p>\n<p><strong>ListDictionary<\/strong><\/p>\n<p>This <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.specialized.listdictionary(v=vs.110).aspx\" title=\"ListDictionary Class\" target=\"_blank\">ListDictionary<\/a> is a simple IDictionary implementation. It uses a singly linked list and it&#8217;s smaller and faster than a Hashtable if the number of elements is 10 or less. <\/p>\n<p><strong>NameValueCollection<\/strong><\/p>\n<p>Then <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.specialized.namevaluecollection(v=vs.110).aspx\" title=\"NameValueCollection Class\" target=\"_blank\">NameValueCollection<\/a> is used to store name\/value pairs, however unlike say a dictionary, nulls can be used for both name and value.<\/p>\n<p><strong>ObservableCollection&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms668604(v=vs.110).aspx\" title=\"ObservableCollection Class\" target=\"_blank\">ObservableCollection<\/a> provide notifications when items are added to or removed from the collection or when the collection is refreshed. Used a lot with binding due to the support for INotifyPropertyChanged and INotifyCollectionChanged.<\/p>\n<p><strong>OrderedDictionary<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.specialized.ordereddictionary(v=vs.110).aspx\" title=\"OrderedDictionary\" target=\"_blank\">OrderedDictionary<\/a> represents key\/value data where the data may be accessed via the key or the index. <\/p>\n<p><strong>Queue&lt;T&gt;<\/strong><\/p>\n<p>A <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/7977ey2c(v=vs.110).aspx\" title=\"Queue Class\" target=\"_blank\">Queue<\/a> is a FIFO collection. Whereby items are added to the back of the queue (using Enqueue) and taken from the front of the Queue using (Dequeue). A queue ultimately stores it&#8217;s elements in an array.<\/p>\n<p><strong>ReadOnlyCollection&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms132474(v=vs.110).aspx\" title=\"ReadOnlyCollection Class\" target=\"_blank\">ReadOnlyCollection<\/a> class is for a generic read-only\/immutable collection. <\/p>\n<p><strong>ReadOnlyDictionary&lt;TKey, TValue&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/gg712875(v=vs.110).aspx\" title=\"ReadOnlyDictionary Class\" target=\"_blank\">ReadOnlyDictionary<\/a> is a class for generic key\/value pairs in a read-only\/immutable dictionary.<\/p>\n<p><strong>ReadOnlyObservableCollection&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms668620(v=vs.110).aspx\" title=\"ReadOnlyObservableCollection Class\" target=\"_blank\">ReadOnlyObservableCollection<\/a> is a class for a generic read-only\/immutable observable collection. Changed may be made to the underlying observable collection but not to the ReadOnlyObservableCollection wrapper.<\/p>\n<p><strong>SortedDictionary&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/f7fta44c(v=vs.110).aspx\" title=\"SortedDictionary Class\" target=\"_blank\">SortedDictionary<\/a> is a dictionary where items are sorted on the key.<\/p>\n<p><strong>SortedList&lt;TKey, TValue&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms132319(v=vs.110).aspx\" title=\"SortedList Class\" target=\"_blank\">SortedList<\/a> is a collecion of key\/value pairs that are sorted by key. A SortedList uses less memory than a SortedDictionary, but the SortedDictionary has faster insertion and removal of items.<\/p>\n<p><strong>SortedSet&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd412070(v=vs.110).aspx\" title=\"SortedSet Class\" target=\"_blank\">SortedSet<\/a> is a collection where items are maintained in sorted order. Duplicate items are not allowed.<\/p>\n<p><strong>Stack&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/3278tedw(v=vs.110).aspx\" title=\"Stack Class\" target=\"_blank\">Stack<\/a> is standard LIFO (last-in first-out) collection. Items are pushed onto the top of the Stack and popped off of the top of the stack.<\/p>\n<p><strong>StringCollection<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.specialized.stringcollection(v=vs.110).aspx\" title=\"StringCollection Class\" target=\"_blank\">StringCollection<\/a> is a specialization of a standard collection for handling strings.<\/p>\n<p><strong>StringDictionary<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.collections.specialized.stringdictionary(v=vs.110).aspx\" title=\"StringDictionary Class\" target=\"_blank\">StringDictionary<\/a> is a specialization of a standard dictionary for handling strings.<\/p>\n<p><strong>SynchronizedCollection&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms668265(v=vs.110).aspx\" title=\"SynchronizedCollection\" target=\"_blank\">SynchronizedCollection<\/a> is a thread-safe collection. It&#8217;s part of the System.ServiceModel assembly and used within WCF.<\/p>\n<p><strong>SynchronizedKeyedCollection&lt;TKey, TValue&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms668294(v=vs.110).aspx\" title=\"SynchronizedKeyedCollection Class\" target=\"_blank\">SynchronizedKeyedCollection<\/a> is a thread-safe key\/value type collection. It&#8217;s abstract, so is designed to be overloaded for your specific needs. . It&#8217;s part of the System.ServiceModel assembly and used within WCF.<\/p>\n<p><strong>SynchronizedReadOnlyCollection&lt;T&gt;<\/strong><\/p>\n<p>The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms404554(v=vs.110).aspx\" title=\"SynchronizedReadOnlyCollection Class\" target=\"_blank\">SynchronizedReadOnlyCollection<\/a> is a thread-safe read-only collection. This is part of the System.ServiceModel assembly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been writing C#\/.NET code for a long time, but every now and then I feel the need to give myself a refresher on the basics and it usually results in finding features that have been added which I knew nothing about. You know how you get used to using classes so look no further. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1337","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=1337"}],"version-history":[{"count":33,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1337\/revisions"}],"predecessor-version":[{"id":1398,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1337\/revisions\/1398"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}