Collections in C# and .NET Framework's System.Collections class

.NET Framework utilizes all of the standard collections and more. Below is the list of all available collections in ASP.NET

 

NameDescription of Collections
ArrayList index-based
SortedList sorted
Queue first-in, first-out
Stack last-in, first-out
Hashtable name/value pairs
BitArray collection of Boolean values
StringCollection resizable
StringDictionary collection of name/values pairs of strings
ListDictionary collection of small lists
HybridDictionary collection that uses a ListDictionary
NameValueCollection collection of name/values pairs of strings

ArrayList class is simplistic and unordered array of any object type. In order to add to an ArrayList we use two methods: Add and AddRange
ArrayList myCollection = new ArrayList();
// Add individual items to the collection
string myString = "Hello World";
myCollection.Add(myString);
myCollection.Add("Hi World");
myCollection.Add(50);
myCollection.Add(new object());

If we decided to add a range of values then we use AddRange, we can add range of strings or objects just like with Add method.
string[] myArray = new string[] { "one", "two", "three" };
coll.AddRange(myArray);

ArrayList class also support inserts at predefine slot. We use Insert and InsertRange in order to perform that.
myCollection.Insert(3, "My String");
string[] moreMyStrings = new string[] { "First String", "Second String" };
myCollection.InsertRange(4, moreMyStrings);

Alternatively you can do this: Coll[3] = "My Insertion String"; the only difference here is that we are not inserting new record but rather overwriting existing one with the new one. Removing from the ArrayList is supported with the help of Remove, RemoveAt, and RemoveRange methods.
myCollection.Add("Hello");
myCollection.Remove("Hello");
myCollection.RemoveAt(0); // Delete first item
myCollection.RemoveRange(0, 4); // Delete first four items

The Clear method is used to empty a collection of all its items.

The Index Of method is used to determine the index of a particular item in the collection.

The Contains method is used to test whether a particular object exists in the collection.

Iteration is very simple with the ArrayList since it has indexer.
for (int × = 0; × < myCollection.Count; ++×)
{
    Console.WriteLine(myCollection[×]);
}

Another method to iterate through ArrayList comes with the support of IEnumerable interface which imposes following properties and methods

Properties

 

NameDescription
Current Gets the current item

Methods

 

NameDescription
MoveNext Moves to the next item
Reset Sets the enumerator to before the first item

IEnumerator myEnumerator = coll.GetEnumerator();
while (myEnumerator.MoveNext())
{
    Console.WriteLine(myEnumerator.Current);
}

Enumeration is also supported with the foreach loop construct. Advantage of this method is that you can cast at the same time as long as you know what is stored inside ArrayList
foreach (object item in myCollection)
{
    Console.WriteLine(item);
}

ICollection is also support with ArrayList and all Properties and methods are available

Properties

 

NameDescription
Count # of items in the collection
IsSynchronized collection is thread safe indicator
SyncRoot Gets an object that can be used

Methods

 

NameDescription
CopyTo Copies the contents into an Array

IList is another interface support with ArrayList and is derived from ICollection which is in tern derived from IEnumerable.

Properties

 

NameDescription
IsFixedSize Determines an indicator of whether collection can be resized
IsReadOnly Determines an indicator of whether a collection can be changed
Item Determines or sets the item at a specific index in collection

Methods

 

NameDescription
Add Adds an item
Clear Clears all items
Contains Checks if a specific item is contained
IndexOf Finds an item in the collection
Insert Adds an item at a specificied index
Remove Removes the first occurrence of the specified item
RemoveAt Removes an item at a specific index

Sort is easy that works like that: coll.Sort(); Sort method allows to use comparer class that implements IComparer and you will use it like that:
coll.Sort(new CaseInsensitiveComparer());