DOT NET Lab Practicals Programs
1)Write a c# program to implement class and object
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the value of a");
int a = Convert.ToInt16(Console.ReadLine());
Console.Write("Enter the value of b");
int b = Convert.ToInt16(Console.ReadLine());
int c = a + b;
Console.WriteLine("sum is"+c);
c = a - b;
Console.WriteLine("substitute is" + c);
c = a * b;
Console.WriteLine("multiplication is" + c);
double d = (double) a/b;
Console.WriteLine("division is" + d);
}
}
}
Output:-
Enter the value of a =40
Enter the value of b =20
sum is = 60
substitute is =20
multiplication is =800
division is = 2
2) Write a c# program to implement inheritance
namespace consoleApplication
{
class ggits
{
public void add( )
{ int a=10;
int b=20;
int c=a+b;
Console.WriteLine("Sum is:"+c);
}
}
class home : ggits
{
public void show( )
{
Console.WriteLine("Hello GGITS");
}
}
class program
{
static void main(String void main(String [ ] args)
{
home t =new home()
t.show();
t.add();
}
}
}
Output:-
Hello GGITS
Sum is 30
4)(a)Write a c# program to implement polymorphism method overriding
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
namespace OOPS
{
class SuperClass
{
public virtual void display()
{
Console.WriteLine("hELLO sUPERcLASS");
}
}
class Polymorphism : SuperClass
{
public override void display()
{
Console.WriteLine("Hello Subclass");
}
public static void Main()
{
SuperClass S = new Polymorphism();
S.display();
Console.ReadKey();
}
}
}
}
Output:-
Hello Subclass
4)(b)Write a c# program to implement method overloading
namespace OOP
{
class home
{
public void area(int r)
{
double ar1 = 3.14 * r * r;
Console.WriteLine("Area of circle is : " + ar1);
}
public void area(int length, int width)
{
int ar2 = length * width;
Console.WriteLine("Area of rectangle is" + ar2);
}
public void area(double r)
{
int h = 20;
double v = 3.14 * r * r * h;
Console.WriteLine("volume of Cylinder is:" + v);
}
}
class Program
{
static void Main(string[] args)
{
home t = new home();
t.area(10);
t.area(10, 20);
t.area(2.5);
}
}
}
Output:-
Area of circle is : 314
Area of rectangle is : 200
Volume of rectangular is : 392.5
5)Write a c# program to implement abstract class
namespace ConsoleApplication20
{
abstract class home
{
public void show()
{
Console.WriteLine("good morning");
}
public abstract void add(int a, int b);
}
class ggits : home
{
public void multi(int a, int b)
{
int c = a * b;
Console.WriteLine("multiplication is " + c);
}
public override void add(int a, int b)
{
int c = a + b;
Console.WriteLine("sum is" + c);
}
}
class Program
{
static void Main(string[] args)
{
ggits t = new ggits();
t.show();
t.multi(10, 20);
t.add(4, 5);
}
}
}
Output:-
good morning
multiplication is 200
sum is 9
6)Write a c# program to implement Base Keyword
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class Person
{
protected string ssn = "444 -55-6666";
protected string name = "John L. Malgraine";
public virtual void GetInfo()
{
Console.WriteLine("Name: {0}", name);
Console.WriteLine("SSN: {0}", ssn);
}
}
class Employee : Person
{
public string id = "ABC567EFG";
public override void GetInfo()
{
Console.WriteLine("Employee ID: {0}", id);
}
}
class TestClass
{
static void Main()
{
Employee E = new Employee();
E.GetInfo();
}
}
}
Output:-
Employee ID: ABC567EFG
7)Write a c# program to implement interface
namespace ConsoleApplication21
{
interface home
{
void display();
void add();
}
class ggits : home
{
public void display()
{
Console.WriteLine("hello");
}
public void add()
{
int a = 10;
int b = 20;
int c = a + b;
Console.WriteLine("sum is:" + c);
}
}
class Program
{
static void Main(string[] args)
{
ggits t = new ggits();
t.display();
t.add();
}
}
}
Output:-
hello
sum is : 30
8)Write a c# program to implement 2D Array
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
int[,] a = new int[2,2];
a[0, 0] = 10;
a[0, 1] = 20;
a[1, 0] = 30;
a[1, 1] = 40;
Console.WriteLine("Element for the array");
for (int i = 0; i < 2; i++)
{
for (uint j = 0; j < 2; j++)
{
Console.Write(" "+a[i,j]);
}
Console.WriteLine();
}
}
}
}
Output:-
Element for the array
10 20
30 40
9)Write a c# program to implement Array List
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] array)
{
ArrayList number=new ArrayList();
int[] a={10,2,3,5,4,6,7,8,19,13,1};
foreach(int num in a)
{
number.Add(num);
}
Console.Write("Contents of ArrayList are:");
for(int i=0;i<number.Count;i++)
{
Console.Write(" "+(int) number [i]);
}
Console.WriteLine();
number.Add(100);
Console.Write("Content of arrayList (after adding 100):");
for (int i = 0; i < number.Count; i++)
{
Console.Write(" " + (int)number[i]);
}
Console.WriteLine();
number.Remove(19);
Console.Write("Contents of arrayList (after remove 19):");
for(int i=0;i<number.Count;i++)
{
Console.Write(" "+(int) number [i]);
}
Console.WriteLine();
}
}
}
Output:
Contents of ArrayList are: 10 23 5 4 6 7 8 19 13 1
Contents of ArrayList(after adding 100); 10 2 3 5 4 7 8 19 13 1 100
Contents of ArrayList(after remove 19)
10 2 3 5 4 6 7 13 1 100
10) C# Program to Implement Stack with Push and Pop operations
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
stack st = new stack();
while (true)
{
Console.Clear();
Console.WriteLine("\nStack MENU(size -- 10)");
Console.WriteLine("1. Add an element");
Console.WriteLine("2. See the Top element.");
Console.WriteLine("3. Remove top element.");
Console.WriteLine("4. Display stack elements.");
Console.WriteLine("5. Exit");
Console.Write("Select your choice: ");
int choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("Enter an Element : ");
st.Push(Console.ReadLine());
break;
case 2: Console.WriteLine("Top element is: {0}", st.Peek());
break;
case 3: Console.WriteLine("Element removed: {0}", st.Pop());
break;
case 4: st.Display();
break;
case 5: System.Environment.Exit(1);
break;
}
Console.ReadKey();
}
}
}
interface StackADT
{
Boolean isEmpty();
void Push(Object element);
Object Pop();
Object Peek();
void Display();
}
class stack : StackADT
{
private int StackSize;
public int StackSizeSet
{
get { return StackSize; }
set { StackSize = value; }
}
public int top;
Object[] item;
public stack()
{
StackSizeSet = 10;
item = new Object[StackSizeSet];
top = -1;
}
public stack(int capacity)
{
StackSizeSet = capacity;
item = new Object[StackSizeSet];
top = -1;
}
public bool isEmpty()
{
if (top == -1) return true;
return false;
}
public void Push(object element)
{
if (top == (StackSize - 1))
{
Console.WriteLine("Stack is full!");
}
else
{
item[++top] = element;
Console.WriteLine("Item pushed successfully!");
}
}
public object Pop()
{
if (isEmpty())
{
Console.WriteLine("Stack is empty!");
return "No elements";
}
else
{
return item[top--];
}
}
public object Peek()
{
if (isEmpty())
{
Console.WriteLine("Stack is empty!");
return "No elements";
}
else
{
return item[top];
}
}
public void Display()
{
for (int i = top; i > -1; i--)
{
Console.WriteLine("Item {0}: {1}", (i + 1), item[i]);
}
}
}
}
Output:-
Stack MENU <size__10>
1.Add an element
2.See the top element
3.Remove Top element
4.Display stack element
5.Exit
Select your Choice
11) C# Program to Implement Queue
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("five");
foreach (string number in numbers)
{
Console.WriteLine(number);
}
Console.WriteLine("\nDequeuing '{0}'", numbers.Dequeue());
Console.WriteLine("Peek at next item to dequeue: {0}",
numbers.Peek());
Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue());
Queue<string> queueCopy = new Queue<string>(numbers.ToArray());
Console.WriteLine("\nContents of the first copy:");
foreach (string number in queueCopy)
{
Console.WriteLine(number);
}
string[] array2 = new string[numbers.Count * 2];
numbers.CopyTo(array2, numbers.Count);
Queue<string> queueCopy2 = new Queue<string>(array2);
Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
foreach (string number in queueCopy2)
{
Console.WriteLine(number);
}
Console.WriteLine("\nqueueCopy.Contains(\"four\") = {0}",
queueCopy.Contains("four"));
Console.WriteLine("\nqueueCopy.Clear()");
queueCopy.Clear();
Console.WriteLine("\nqueueCopy.Count = {0}", queueCopy.Count);
}
}
}
Output:-
one
two
three
four
five
Dequeuing 'one'
Peek at next item to dequeue: two
Dequeuing 'two'
Contents of the copy:
three
four
five
Contents of the second copy, with duplicates and nulls:
three
four
five
queueCopy.Contains("four") = True
queueCopy.Clear()
Comments
Post a Comment