.NET collections C# tutorial. Understanding Collections

NET COLLECTIONS TUTORIAL

C# Tutorials

  

NET COLLECTIONS TUTORIAL

Learn Tutorials
.NET collections C# tutorial. Understanding Collections

Example code for NET collections tutorial:
.NET Collections

From a .NET perspective, a collection could be defined as an object that implements one or more of the System.Collections.ICollection, System.Collections.IDictionary, and System.Collections.IList interfaces.

Types of collections:

Ordered collections

- implements ICollection (Count, IsSynchronized, SyncRoot, CopyTo)
- retrievable only by enumerating thru the sort order
- insertion order controls retrieval order
BitArray
Stack
Queue
NameValueCollection

BitArray - Manages a compact array of bit values, which are represented as Booleans, where true
indicates that the bit is on (1) and false indicates the bit is off (0).

Stack - a simple last-in-first-out collection of objects.

Queue - a first-in, first-out collection of objects.

NameValueCollection - a sorted collection of associated String keys and String values that can be
          accessed either with the key or with the index.




Indexed collections "formal list"

- implements IList (IsFixedSize, IsReadOnly, Item, Add, Clear, Contains, IndexOf, Insert, Remove, RemoveAt)
- implements ICollection (Count, IsSynchronized, SyncRoot, CopyTo)
- retrieved via a numeric index (like an array)
- allows indexing directly into the collection
- also supports enumeration
Array
ArrayList
StringCollection

Array - (Abstract) Provides methods for creating, manipulating, searching, and sorting arrays,
thereby serving as the base class for all arrays in the common language runtime.

ArrayList - Implements the IList interface using an array whose size is dynamically increased as
required. (like a vector)

StringCollection - a collection of strings.
       Recommended for: smaller amounts of data that is frequently updated or
added to   




Keyed collections "dictionary" "keyed collections"

- implements IDictionary (IsFixedSize, IsReadOnly, Item, Keys, Values, Add, Clear, Contains, GetEnumerator, Remove)
- implements ICollection (Count, IsSynchronized, SyncRoot, CopyTo)
- key,value pairs
- can be retrieved in sorted order by enumeration
- allows both enumeration and indexing directly into the collection via an associated key
- usually sorted in some fashion based on the key value
HashTable
ListDictionary
SortedList
HybridDictionary
StringDictionary

HashTable - a collection of key-and-value pairs that are organized based on the hash code of
the key.
Recommended for: a large amount of relatively static data will be repeatedly
searched for arbitrary keys.

ListDictionary - Implements IDictionary using a singly linked list. Recommended for collections
that typically contain 10 items or less.

SortedList - a collection of key-and-value pairs that are sorted by the keys and are accessible
by key and by index.

HybridDictionary - Implements IDictionary by using a ListDictionary while the collection is small,
and then switching to a Hashtable when the collection gets large.

StringDictionary - Implements a hashtable with the key strongly typed to be a string rather than
an object.
       recommended for: large amounts of data that will not be frequently added to,
such as a HashTable.


Note about collections:
most store items internally as type System.Object so:
- any time you add a new item to the collection, the runtime has to perform a box (creating a reference to a value type so that it may be referred to as an object) or downcast operation. That's a little inefficient and will create some performance issues on very large collections.
- any time you access an item in a generic collection, it will be returned as type System.Object, meaning you'll have to upcast it back to its true type


Note on using Arrays vs. Collections
- Arrays will generally be more efficient in access speed and memory usage than any collection, and in many cases will be all you need
Collections are probably only better to use when:
• The number of objects can shrink or grow unpredictably
• The objects you’ll be storing aren’t of all the same type
• You need to perform some processing on the object when it is added to or retrieved from the array.
This is example code for .net collections c# tutorial. understanding collections