Collection is the abstractions of data algorithms.Net offer
different kinds of collection like Array list Hash table, Dictionaries, queues.
ArrayList Collection –
An Arraylist is just like a dynamic array, each element of arraylist is
accessed by the using indexing operator. In Arraylist we can add or delete item
at run time.
The syntax of the ArrayList is given below:
Arraylist obj=new ArrayList();
We can add item by using the Add() method
Obj.Add(“Sachin”);
Obj.Add(“Sharad”);
We can remove the item by using Remove() method
Obj.Remove(“sachin”);
Example :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
ArrayList obj = new
ArrayList();
obj.Add("sachin");
obj.Add("Sharad");
obj.Add("Pramod");
obj.Insert(2, "Amit");
for (int i = 0; i
< obj.Count; i++)
{
Console.WriteLine("At Index Value[" + i + "]= " + obj[i].ToString());
}
Console.ReadKey();
}
}
}
output :
Hashtable:
Hashtable works just like of ArrayList except that it is not
required to use a numerical index.
The Syntax of the Hashtable is given below:
Hashtable obj = new Hashtable();
Benefits of Hashtable
:
We can add many pairs of key/value element as required , we
do not have to specify the size at a time
We can use text, number, dates as per the index.
It is very fast element.
We can remove any element very easily.
Limitations of
Hashtable :
It is required unique key
Sorting is not done by the key/value
The example of the HashTable is given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Hashtable obj = new
Hashtable();
obj[Convert.ToDateTime("01/31/1990")] = "My Birth Date";
obj[Convert.ToDateTime("01/31/1990")] = "My First Birthday";
obj[Convert.ToDateTime("01/31/2016")] = "My 26th Birthday";
Console.WriteLine("Enter
date 'Month/Date/Year' Format");
DateTime DateData = DateTime.Parse(Console.ReadLine());
Console.WriteLine(obj[DateData]);
Console.ReadKey();
}
}
}
Output :
No comments:
Post a Comment