Professional Documents
Culture Documents
Unit – IX
Collection Classes
➢ IList interface –
IList is non-generic collection object that can be individually access by index. IList
interface has implemented from two interfaces and they are ICollection and IEnumerable. The
definition of IList interface is like –
It is derived from the interface ICollection and defines additional properties and methods. The
major reason why the Array class implements the IList interface is that IList interface defines Item
property for accessing the elements using an indexer.
➢ IDictionary Interface –
The interface IDictionary is implemented by non-generic collections whose elements
have a key and a value. It is similar to IList but members are accessed by key value rather than index.
Syntax –
IDictionary<int, string> object_name = new IDictionary <int, string> ();
➢ Collection Classes –
Collection classes can be grouped into collection that store elements of type object and
generic collection classes.
Collection is a set of similar type of elements that are grouped together. Collections can be
grouped into lists, collections and dictionaries based on interfaces that are implemented by collection
class.
Collection has following type –
Collection
➢ ArrayList Class –
Array in C# have a fixed size once they are initialized. If user need to dynamically
increase the size of an array during runtime it generate exception.
ArrayList class mainly used to dynamically increase the size of an array during runtime. You
can use it as like array but its size can be increased dynamically as required. ArrayList class
represents a list which could be resized dynamically. Indexes in ArrayList collection is zero based.
ArrayList class is located within the System.Collectios namespace. Each item in the ArrayList
is a generic object System.Object type. The ArrayList class implements the IList interface. For
searching, sorting, insertion, reversing, or deleting elements in the list. To use the ArrayList class you
first create an instance of that class like as-
3) Insert () – Insert () method is used to insert an elements into the ArrayList at the specified index.
Syntax – public virtual void Insert (int index, object value)
4) Sort () – Sort the elements in the entire ArrayList using the IComparable implementation of each
elements.
Syntax – public void Sort ()
5) Contains () – Contains () method is used to check the specified object is present in list or not. It
return true if object is present in list otherwise it return false.
8) RemoveAt () – RemoveAt () method removes the elements at the specified index position
Syntax – public virtual void RemoveAt (int index)
Properties –
1) Count – Count property returns the number of elements actually contained in the ArrayList
collection.
2) Capacity – Gets or sets the number of elements that the ArrayList can contain. The default
capacity of an ArrayList is 4. If we add 1 elements it become capacity double that is 8
3) IsReadOnly – This property will indicate whether the ArrayList is read only or not.
Example –
using System;
using System.Collections;
namespace ExampleArrayList
{
class Program
{
static void Main(string[] args)
{
ArrayList myarray = new ArrayList();
myarray.Add("CPP");
myarray.Add("Java");
myarray.Add("PHP");
myarray.Add("Math");
myarray.Add("DBMS");
Console.WriteLine("Items are added in ArrayList");
Console.WriteLine("\nArrayList contain items these are ---");
foreach (object obj in myarray)
Console.WriteLine(obj.ToString());
myarray.Insert(3,"Linux");
myarray.Remove("Math");
Console.WriteLine("\nAfter Removing the ArrayList is---");
foreach (object obj in myarray)
Console.WriteLine(obj.ToString());
myarray.Sort();
Console.WriteLine("\nAfter Sorting the ArrayList is---");
foreach (object obj in myarray)
Console.WriteLine(obj.ToString());
myarray.Reverse();
Console.WriteLine("\nAfter Using reverse method the list is---");
foreach (object obj in myarray)
Console.WriteLine(obj.ToString());
Console.ReadLine();
}
}
}
Output –
Items are added in ArrayList
ArrayList contain items these are ---
CPP
Java
PHP
Math
DBMS
➢ HashTable Class –
HashTable represents a collection of key or value pairs that are organized based on the
hash code of the key.
Each element is a key or value pair should in a DictionaryEntry object. The HashTable
collection is similar to ArrayList but it allows accessing each elements in the collection using
a key. Each element is a key and value pair stored in a DictionaryEntry object. The key is used to
access the items in the collection.
The HashTable class provides different methods and properties to manipulate data –
Methods –
1) Add () – Add () method adds an element with the specified key and value into the HashTable.
Syntax – public virtual void Add (object key, object value)
3) ContainsKey () – This method determines whether the HashTable contains specific key.
Syntax – public virtual bool ContainsKey (object key)
4) ContainsValue () - This method determines whether the HashTable contains specific value.
Syntax – public virtual bool ContainsValue (object value)
5) Remove () – This method is used to remove the elements with specified key from the HshaTable
Syntax – public virtual void Remove (object key)
Property –
1) Count – Count property return the number of key and value pair contained in the HashTable.
2) Item – Gets or sets the value associated with the specified key.
5) IsReadOnly - This property will indicate whether the HashTable is read only or not.
Example –
using System;
using System.Collections;
namespace HashTableEx
{
class Program
{
static void Main(string[] args)
{
Hashtable table = new Hashtable();
table.Add("B1","CPP");
table.Add("B2","PHP");
table.Add("B3","Java");
table.Add("B4","SE");
Console.WriteLine("Objects are added in the HashTable");
Console.WriteLine("HashTable Values are ---");
foreach (object obj in table.Values)
Console.WriteLine(obj.ToString());
Console.WriteLine("\nHashTable contain---");
foreach (DictionaryEntry de in table)
Console.WriteLine(de.Key + " : " + de.Value);
table.ContainsValue("DBMS"));
Console.WriteLine("\nThe HashTable contain items are: "+table.Count);
table.Remove("B1");
Console.WriteLine("\nAfter removing, the HashTables elements are--");
foreach (DictionaryEntry de1 in table)
Console.WriteLine(de1.Key + " : " + de1.Value);
Console.ReadLine();
}
}
}
Output -
Objects are added in the HashTable
HashTable Values are ---
Java
PHP
CPP
SE
HashTable contain---
B3 : Java
B2 : PHP
B1 : CPP
B4 : SE