You are on page 1of 7

Shriram Institute of Information Technology, Paniv

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 –

public Interface IList : ICollection, IEnumerable

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

Array Advanced Collection

Non-Generic Collection Generic


E.g. – ArrayList,
HashTable, Stack, Queue

Prof. Honrao Charushila Prakash (1 )


Shriram Institute of Information Technology, Paniv

➢ 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-

ArrayList <instance_name> = new ArrayList;


Example –
ArrayList myarray = new ArrayList;

Some important point related ArrayList is –


1) To use ArrayList class you need to add a System.Collectios namespace
2) ArrayList implements the System.Collections.IList interface using an array whose size is
dynamically increased as required.
3) ArrayList is an alternate of an Array but you can’t implement multi dimensional array using
ArrayList.
4) ArrayList size can be dynamically increased or decreased.
5) ArrayList is collection of variables of the same type or multiple types.
6) The capacity of an ArrayList is the number of elements the ArrayList can hold.
7) Elements in an ArrayList collection can access using an integer index.
8) ArrayList collection accepts null as a valid value, allows duplicate elements.

ArrayList class provides different methods and properties to manipulate data –


Methods –
1) Add () – Add() method is used to add object or elements to the end of the ArrayList
Syntax – public virtual int Add (object name);

2) Remove () – Remove () method is used to removes elements from the ArrayList.


Syntax – public virtual void Remove (object obj)

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.

Prof. Honrao Charushila Prakash (2 )


Shriram Institute of Information Technology, Paniv

Syntax – Contains (object value)

6) Clear () – It used to remove all elements from the ArrayList


Syntax – public virtual void Clear ()

7) Reverse () – Reverse the order of the elements in the entire collection.


Syntax – public virtual void Reverse ()

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.

4) Items – It will return the particular items of specified index.

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");

Prof. Honrao Charushila Prakash (3 )


Shriram Institute of Information Technology, Paniv

Console.WriteLine("\nAfter Inserting item the ArrayList is---");


foreach (object obj in myarray)
Console.WriteLine(obj.ToString());

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());

Console.WriteLine("\nContain the element: "+myarray.Contains("Math"));

Console.WriteLine("\nArrayList contain items: " + myarray.Count);

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

After Inserting item the ArrayList is---


CPP
Java
PHP
Linux
Math
DBMS

Prof. Honrao Charushila Prakash (4 )


Shriram Institute of Information Technology, Paniv

After Removing the ArrayList is---


CPP
Java
PHP
Linux
DBMS

After Sorting the ArrayList is---


CPP
DBMS
Java
Linux
PHP

Contain the element: False

ArrayList contain items: 5

After Using reverse method the list is---


PHP
Linux
Java
DBMS
CPP

➢ 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)

2) Clear () – It used to remove all elements from the HashTable.


Syntax – public virtual void Clear ()

3) ContainsKey () – This method determines whether the HashTable contains specific key.
Syntax – public virtual bool ContainsKey (object key)

Prof. Honrao Charushila Prakash (5 )


Shriram Institute of Information Technology, Paniv

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.

3) Keys – Gets an ICollection containing the keys in the HashTable.

4) Values - Gets an ICollection containing the values in the HashTable.

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);

Console.WriteLine("\nThe key is contain in HashTable: " +


table.ContainsKey("B3"));
Console.WriteLine("\nThe Value is contain in HashTable: " +

Prof. Honrao Charushila Prakash (6 )


Shriram Institute of Information Technology, Paniv

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

The key is contain in HashTable: True

The Value is containing in HashTable: False

The HashTable contain items are: 4

After removing, the HashTable elements are--


B3 : Java
B2 : PHP
B4 : SE

Prof. Honrao Charushila Prakash (7 )

You might also like