Progrma7 10
Progrma7 10
Write a Program in C# to create and implement a Delegate for any two arithmetic operations
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication7
{
class calculate
{
public float add(float a, float b)
{
return (a + b);
}
public float sub(float a, float b)
{
return (a - b);
}
public float mul(float a, float b)
{
return (a * b);
}
public float quo(float a, float b)
{
return (a / b);
}
public float mod(float a, float b)
{
return (a % b);
}
}
public delegate float calculatordelegate(float a,float b);
class Program
{
static void Main(string[] args)
{
calculate c = new calculate();
calculatordelegate cd = new calculatordelegate(c.add);
cd += c.sub;
Console.WriteLine("The difference is:" + cd(a, b));
cd += c.mul;
Console.WriteLine("The product is:" + cd(a, b));
cd += c.quo;
Console.WriteLine("The quotient is:" + cd(a, b));
cd += c.mod;
Console.WriteLine("The remainder is:" + cd(a, b));
}
}
}
10. Write a Program in C# Demonstrate arrays of interface types (for runtime polymorphism).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication10
{
interface shape
{
double cal_area();
}
class circle:shape
{
public double cal_area()
{
Console.WriteLine();
Console.Write("Enter the radius:");
double r = double.Parse(Console.ReadLine());
double area=3.14 * r * r;
return area;
}
}
class triangle:shape
{
}
class square:shape
{
}
class rectangle:shape
{
public double cal_area()
{
Console.WriteLine();
Console.Write("Enter the length and breadth of a rectangle:");
double l = double.Parse(Console.ReadLine());
double b = double.Parse(Console.ReadLine());
double area = l *b ;
return area;
}
}
class Program
{
static void Main(string[] args)
{
shape[] s = new shape[4];
s[0] = new circle();
s[1] = new triangle();
s[2] = new square();
s[3] = new rectangle();
}
}
}