1. Write a C# program to add two number using delegate.
using System;
namespace labreport
{
public delegate int
Operation(int x, int y);
internal class Program
{
public int Addition(int a, int b)
{
return a + b;
}
public static void Main(string[] args)
{
Program p = new Program();
Operation obj = new Operation(p.Addition);
Console.WriteLine("Sum: " + obj(34, 56));
Console.ReadKey();
} Output:
}
}
2. Write a C# program to add two digit using constructor.
namespace Contructor{
class Addition{
private int num1, num2;
public Addition(int a, int b){
num1 = a;
num2 = b;
}
public int Add()
{
return num1 + num2;
}
}
class Program{
public static void Main(string[] args)
{
Addition obj = new Addition(3, 6);
Console.WriteLine("Sum: " + obj.Add());
Console.ReadKey();
}
Output:
}
}
3. Write a C# program to display student id and name using automatic properties.
namespace studentDetails{ class Student{
public int Id { get; set; }
public string Name { get; set; }
}
class Program
{
public static void Main(string[] args)
Student st = new Student();
st.Id = 101;
st.Name = "John Doe";
Console.WriteLine($"Id={st.Id} and Name={st.Name}");
Console.ReadKey();
} Output:
}
}
4. Write a C# program to reverse element of an array.
using System;
namespace reverseElemet{
class Program{
public static void Main(string[] args){
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8 };
Console.WriteLine("Original Array");
foreach(int num in numbers){
Console.Write(num + ", ");
}Array.Reverse(numbers);
Console.WriteLine("\nReverse numbers");
foreach(int num in numbers){
Console.Write(num + ", ");
Console.ReadKey();
Output:
}
5. Write a C# program how name of students stored and retrieved using indexer.
using System;
namespace indexerClass{
class Program{
class Indexer{
private string[] name = new string[5];
public string this[int i]{
get{return name[i];
}
set{name[i] = value;
}
}}static void Main(string[] args){
Indexer teams = new Indexer();
teams[0] = "Ram";
teams[1] = "Aayog";
teams[2] = "Shyam";
teams[3] = "Sameer";
teams[4] = "Subash";
for(int i= 0; i<5; i++)
{
Console.WriteLine(teams[i]); Output:
}
Console.ReadKey();
} } }
6. Write a C# program for showing single, multiple, multilevel and hierarchical
inheritance.
namespace inheritance{
class Person{
public void DisplayPerson(){
Console.WriteLine("This is a person class");
}}
//single inheritance
class Student : Person{
public void DisplayStudent(){
Console.WriteLine("single inheritance class");}}
//multilevel inheritance
class Graduation : Student{
public void DisplayGraduation(){
Console.WriteLine("Multilevel inheritance class");
}}
//hiearchical inheritance
class Teacher : Person{
public void DisplayTeacher(){
Console.WriteLine("Hiearchical inheritance class");
}}
//multiple inheritance
interface IWrite{
void Write();
}interface IRead{void Read();
}class Librarain : IWrite, IRead{
public void Write(){
Console.WriteLine("Writing ");
}public void Read(){
Console.WriteLine("Reading ");}}
class Program{
public static void Main(string[] args){
Student st = new Student();
st.DisplayStudent();
Graduation g = new Graduation();
g.DisplayGraduation();
Teacher t = new Teacher();
t.DisplayTeacher();
Librarain lib = new Librarain();
Console.WriteLine("Multiple inheritance");
lib.Write(); Output:
lib.Read();
Console.ReadKey(); }}}
7. Calculate are of rectangular using multiple inheritance in C#.
namespace rectagleExample{
interface ILength{
void SetLength(double length);
}
interface IBreath{
void SetBreath(double breath);
}class Rectangle : ILength, IBreath{
private double length;
private double breath;
public void SetLength(double length){
this.length = length;
}public void SetBreath(double breath){
this.breath = breath;
}public double CalculateArea(){
return length* breath;}}
class Program{ public static void Main(string[] args){
Rectangle rect = new Rectangle();
rect.SetLength(6);
rect.SetBreath(5);
double area = rect.CalculateArea();
Console.WriteLine($"Area of rectangle = {area}");
Console.ReadKey();}}}
Output:
8. Write a C# program how virtual method are used on polymorphism.
namespace polymorphismExample{
class Animal{
public virtual void Speak(){
Console.WriteLine("Animal make sound");
}}class Dog : Animal{
public override void Speak(){
Console.WriteLine("Dog barks");
}}class Cat : Animal{
public override void Speak(){
Console.WriteLine("Cat meows");
}}class Program{
public static void Main(string[] args){
Dog dog = new Dog();
dog.Speak();
Cat cat = new Cat();
cat.Speak();
Console.ReadKey();
}}}
Output: