Collections

Collections can be very useful and can also be used for data binding.
A collection class is used to group objects together.
There are a number of different types of collection classes.
An array is just a special type of collection class.
Some of these collections can hold any type of object whereas others are strongly typed.
Collection classes provide members to add, remove, find and retrieve items
Collection classes also expand automatically when more items are added.


Different Types of Collections

There are two different types of collections
Standard (or generalized) - (ArrayList, BitArray, Hashtable, Queue, SortedList, Stack)
Generic (or specialized) - (Dictionary, HashSet, LinkedList, List Queue, SortedSet, SortedDictionary, SortedList, Stack)


Standard

Added in .NET 1.1
These are the main collection classes and can all be found in the System.Collections namespace


Generic Collections

Added in .NET 2.0
You should always try and program against an interface and not a class implementation
When the class is instantiated the type must be specified
Makes it easier to create type safe collections
These are special versions that allow you to create strongly typed collections.




Collection Class

The ArrayList and Collection classes are very similar.
The Collection class however overloads the Item property to take a string as a key into the collection.
This allows the Collection class to act as a dictionary, associating keys with values.
You can also use the Item property to access members of the collection by index value, however the Collection uses a 1-based index rather than 0-based.


System.Collections.Generic.ICollection<T>

This is the common interface for all the different collection objects.
You can add an item to the collection
You can remove/clear all the items from a collection
You can check if an item/value exists in a collection
You can copy all the items in the collection to an array
You can get the total number of items in a collection
You can remove the first occurrence of a given value
You can check if a collection is read only


System.Collections.Generic.IList<T>

This is the common interface for indexable collections
This contains all the methods of ICollection<T> and IEnumerable<T>
You can get the value (or set the value) at any given position
You can find the index of a specific item
You can insert an item at any position
You can remove an item from any position


System.Collections.Generic.IDictionary<TKey, TValue>

This is the common interface for key/value pair collections
You can check if a given key exists
You can check if a given key and value exists
You can add an entry pair to the dictionary
You can remove an entry from the dictionary
You can get the value corresponding to a given key
You can return the set of keys in the dictionary
You can set the values in the dictionary

Array 
ArrayListThis can work with any type of data (objects, strutcs or primitive types)
Doesn't have a (key,value) relationship
BitArray 
CaseInsensitiveComparer 
CaseInsensitiveHashCodeProvider 
CollectionBase 
Comparer 
DictionaryBase 
DictionaryEntry 
HashTable 
ICollectionimplemented by all collections
IComparercompares two objects, used for sorting
IDictionaryfor key/value based collections
IEnumerabledesignates a class that can be enumerated
IEnumeratordesignates a class that iterates over a collection, supports for each loops
IEqualityComparer 
IHashCodeProvider 
IListused by collections that can be indexed
QueueFirst in, first out collection
ReadOnlyCollectionBase 
SortedList 
StackLast in, first out collection


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopNext