ankitweblogic logo
W e b t u t o r i a l s

Visual Studio.NET IDE

Define C#.NET

C# Comment

C# Variables

C# Data Types

C# Escape Sequence

C# Operators


Format String

Operator Precedence

C# Keywords

Constant Variable

Type Conversion

Flow Control

Exercise Loops & Nested Loop

C# Arrays

C# Strings

User-Define Methods

Variable Scope

C# Enumerations

C# Structure

C# Exception Handling

Object Oriented Programming

C# Classes

Constructor & Destructor

C# Inheritance

C# Polymorphism

C# Operator Overloading

C# Method Overriding

C# Interface

Abstract Classes & Methods

Sealed Classes, Methods

C# Properties

C# Indexer

C# Delegates

C# Generics

C# Collection


C#.Net Collection

The size of arrays is fixed. If the number of elements is dynamic, you should use a collection class.
Collection represents a set of objects that you can access by stepping through each element. The .NET Framework provides specialized classes for managing collection and these classes have rich capability for enhancing your programming experience through better performance and easy maintenance. Object class is the base class of every type in .NET. All the collections implement IEnumerable interface that is extended by ICollection interface. IDictionary and IList are also interfaces for collection which are derived from ICollection.

Various Classes in Collection Namespace:

The System.Collections.Stack class is a kind of collection that works on the principle of Last In First Out (LIFO), means that the item that is added last is executed first. The insertion of an item onto the Stack is termed as 'Push', Removing an item from the stack is called 'Pop'. If the item is only read from the top of the stack, then this is called a 'Peek' operation.

Example for Stack Method: push(), pop(), peek(), count()
	Stack st = new Stack();
	foreach(int i in st)

	Stack < int > st = new Stack < int > ();
	foreach(int i in st)

The System.Collections.Queue class works on the principle of First In First Out (FIFO), means that the item inserted first is processed first. To insert an item is termed as Enqueue, and removal of an item from the queue is termed as 'Dequeue'. Eg: printing.

Queue < int > qu = new Queue < int >();
foreach(int i in qu)

Linked List:
A linked list is a linear collection of self-referential class objects called nodes, connected by reference links. Data is stored in a linked list dynamically--that is, each node is created as necessary.
Advantage of Linked List is:
Linked lists provide better memory utilization because they can grow and shrink at execution time thus, saving memory.

LinkedList < int > llist = new LinkedList < int >();
	llist.AddAfter(llist.Last ,20);
	llist.AddAfter(llist.Last, 30);
	llist.AddAfter(llist.Last, 40);
	foreach (int i in llist )

The System.Collection.ArrayList class is similar to arrays, but can store elements of any data type. We don't need to specify the size of the collection when using an ArrayList. The size of the ArrayList grows dynamically as the number of elements changes.

ArrayList list = new ArrayList();
	ArrayList list = new ArrayList(20);

static void Main()
	ArrayList list = new ArrayList();
	foreach(int num in list)

arrayList.Sort(); // sort the array list.

List of some other important properties and methods of the ArrayList class is:
Property Or Method Description
Capacity Gets or sets the number of elements the ArrayList can contain
Count Gets the exact number of elements in the ArrayList
Add(object) Adds an element at the end of an ArrayList
Remove(object) Removes an element from the ArrayList
RemoveAt(int) Removes an element at the specified index from the ArrayList
Insert(int, object) Inserts an object in the ArrayList at the specified index.
Clear() Removes all the elements from the ArrayList

Dictionary is a collection that store items in a key-pair manner. Each value in the collection is identified by its key and will be unique. Similar to English dictionary where each word (Key) has its corresponding meaning (value). The two common types of Dictionary in the System.Collections namespace are Hash table and SortedList.

Hashtable stores items as key-value pairs. A hash table stores the key and its value as an object type.

Hashtableht = new Hashtable();  
Hashtableht = new Hashtable(20);

Adding items to a Hashtable:
	Ht.Add("1", "Ram");
	Ht.Add("2", "Shyam");
	Ht.Add("3", "Sita");
	Ht.Add("4", "Gita");

Removing a particular item

Display values in HashTable:
foreach( string key in ht.keys)

foreach(String value in ht.Values)

Checking for the existence of a particular item in a hashtable

The SortedList class is similar to the Hashtable but the difference, that the items are sorted according to the key. 
	SortedListslist = new SortedList();
	slist.Add("1", "Anita");
	slist.Add("2", "Sunita");
	slist.Add("3", "Sita");
	slist.Add("4", "Gita");

	if (slist.ContainsValue("Neha"))
		Console.WriteLine("This student name is already in the list");
		slist.Add("5", "Neha");

	// get a collection of the keys. 
	ICollection key = slist.Keys;

	foreach (string k in key)
		Console.WriteLine(k + ": " + slist[k]);
Updated: 08-Feb-19