Collections in C#

Posted on Updated on

Collections in C#

Very first question arises here that what are collections?

In general terms collection means a bundle, group, assembly, set, pool or many such like synonyms can be taken into context when one person defining collections. In C# a same functionality is being provided by Collections which is again the part of System. Collections Namespace.

There are basically two means to assemble objects into one group: One alternative is arrays, second alternative is with the help of Collections. Collections provides helps to fabricate stacks, queues and linked lists. These classes served various tenacities, like memory allocation at run time to elements and reading the value of elements using their index number. Some of the common classes used in Collection are: Array, Collection, Hash table, Stack, Queues and many more.

It holds citations to the various objects. Now some the collections has been discussed below:

1. Array List: Array list unlike arrays can stores data of various types. One don’t have to concern about the data types that self wants to save into the Array list.

2. Hash table: It is another such collection which not only stores info but also stores index number for that info. For example, in your book you various contents. If you want to find any topic in the book, then you have two discretions either you can traverse the whole book by shuffling the pages of the book or either can search the intentional topic by its page number which acts like an index in the hash table and which has to be unique.

3. List: List is also to store many info in single group but having one curb that you can’t access the elements directly with the help of key as there is no such catalogue associated with the list.

Below is one program to demonstrate the usage of Collections:

using System;

using System.Collections;

//Program to show use of collections Hashtable

//Collections provide advanced functionality for managin a group of objects

//Unlike arrays collections can be resized dynamically, members can added and removed at runtime

//System.collections namespace offers a set of interfaces and classes that help users to perform operations on set

//There are various classes in it like arraylist,hashtable,sortedlist

//arraylist supports you to add dynamically or remove dynamically from a list

//hashtable represents collection of keys and values that can be organized on the basis of key

//Sorted list is the organized group of objects and it allows to access them either via key or index

//Linear search 12 4 135 94 4 3 4 678 num=94 avergae best worst

//binary search 11 12 13 14 15 16 17 18 19 num=18 divide and conquer big + end/2=9/2=4 15

namespace Intro_Collections

{

class Program

{

static void Main(string[] args)

{

//First create a new HashTable object from hashtable class

Hashtable obj = new Hashtable();

//Adding values to this object

//It contains two values key, index

//obj,add(value{key},index);

obj.Add(“David”, 101);

obj.Add(“RAMAN”, 103);

//Total number of elements entered

Console.WriteLine(“Total number of elements you entered are: {0}”, obj.Count.ToString());

//Using For each Loop

//Dictionary entry is a ketyword and a is an object of its type use to print value and key

foreach (DictionaryEntry a in obj)

{

Console.WriteLine(“Key={0}, Value={1}”, a.Key, a.Value);

}

Console.ReadKey();

}

}

}

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Leave a comment