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

Console C#.Net Tutorial

Visual Studio.NET IDE

Define C#.NET

C# Comment

C# Variables

C# Data Types

C# Escape Sequence

C# Operators

Exercise-1

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

System.Object

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 are:

ArrayList

The System.Collection.ArrayList class is similar to arrays, but can store elements of any data type. We can declare ArrayList collection with or without specifies its size. The size of the ArrayList can grow dynamically as the number of elements increases.

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

static void Main()
{
    ArrayList list = new ArrayList();
    list.Add(10);
    list.Add(20);
    list.Add(30);
    foreach(int num in list)
    {
        Console.WriteLine(num);
    }
}
List some important properties and methods of ArrayList
S.No.PropertyDescription
CapacityGets or sets the number of elements the ArrayList can contain.
CountGets the exact number of elements in the ArrayList.
S.No.MethodDescription
arrayList.Sort()sort the array list.
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.

Stack

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.

List some important methods of Stack
S.No.MethodDescription
push()insertion of an item onto the Stack.
pop()Removing an item from the stack.
peek()To read/display the top element of the stack.
count()count the number of elements.
Stack st = new Stack();
st.Push(10);
st.Push(20);
st.Push(30);
Console.WriteLine(st.Count());
foreach(int i in st)
{
    Console.WriteLine(i);
}
Stack <int> st = new Stack <int>();
st.Push(10);
st.Push(20);
st.Push(30);
Console.WriteLine(st.Count());
foreach(int i in st)
{
    Console.WriteLine(i);
}

Queue

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: send print request to printer.

Queue < int > qu = new Queue < int >();
qu.Enqueue(10);
qu.Enqueue(20);
qu.Enqueue(30);
qu.Dequeue();
Console.WriteLine(qu.Count());
foreach(int i in qu)
{
	Console.WriteLine(i);
}

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, 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, saves memory.

LinkedList <int> alist = new LinkedList <int>();
alist.AddFirst(10);
alist.AddAfter(alist.Last ,20);
alist.AddAfter(alist.Last, 30);
alist.AddAfter(alist.Last, 40);
foreach (int i in alist )
{
	Console.WriteLine(i);
}

Dictionary

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

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

Hashtableht = new Hashtable();  
		or
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
Ht.Remove("2");
Display values in HashTable:
foreach( string key in ht.keys)
{
    Console.WriteLine(key);
}

foreach(String value in ht.Values)
{
    Console.WriteLine(Value);
}

Checking for the existence of a particular item in a hashtable
	Ht.ContainsKey("1");
	Ht.ContainsValue("Sita");

SortedList

The SortedList class is similar to the Hashtable but the difference is that the items are in sorted according to the key.

SortedList slist = 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");
else
	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: 12-Aug-19