Practical : 14
Subject : .Net
Aim : Write a C# program that demonstrates the usage of Collections 1) Array List 2) Stack 3) Hash Table
Source Code :
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dotnet008
{
class pra14
{
public void
arrlist()
{
ArrayList
al = new ArrayList();
al.Add("a");
al.Add("b");
al.Add("c");
Console.WriteLine("----------Elements of
arraylist----------------");
foreach
(string i in al)
{
Console.WriteLine("{0} and
index is {1}", i, al.IndexOf(i));
}
Console.WriteLine("Elements of arraylist after removing b");
al.Remove("b");
foreach
(string i in al)
{
Console.WriteLine("{0}
and index is {1}", i, al.IndexOf(i));
}
}
public void
stack()
{
Stack st =
new Stack();
st.Push("abc");
st.Push("xyz");
st.Push("pqr");
Console.WriteLine("----------Elements
of stack--------------------");
foreach
(string i in st)
{
Console.WriteLine(i);
}
Console.WriteLine("Elements of stack after pop");
st.Pop();
foreach
(string i in st)
{
Console.WriteLine(i);
}
}
public void
hashtab()
{
Hashtable
hs = new Hashtable();
hs.Add("a1", "abc");
hs.Add("a2",
"xyz");
hs.Add("a3", "pqr");
Console.WriteLine("----------key and vallue of
hashtable----------");
foreach
(DictionaryEntry i in hs)
{
Console.WriteLine("{0} and {1}",i.Key,i.Value);
}
Console.WriteLine("key and value of hashtable after removing
a3");
hs.Remove("a3");
foreach
(DictionaryEntry i in hs)
{
Console.WriteLine("{0} and {1}",i.Key,i.Value);
}
}
static void
Main(string[] ar)
{
pra14 p1 =
new pra14();
p1.arrlist();
p1.stack();
p1.hashtab();
Console.ReadKey();
}
}
}
Output :
0 comments:
Post a Comment