1
Answer

delegate

Arun Kurmi

Arun Kurmi

11y
1.2k
1
In below code how to tell usre to enter Wright Choise(when user enter choise other than +,- or m or M)

//Steps-1.Declear deligate
//2.make method same signature as delegate
//3.create obj of delegate and pass method
//4.invoke delegate method  //argument taken by delegate method internally compare with actual method
using System;
namespace my{
public delegate int Summition(int a,int b);  //step-1
class Abc{
public static int sum(int c,int d){     //step-2
return c+d;
}
public static int diff(int c,int d){     //step-2
return c-d;
}
public static int max(int c,int d){     //step-2
if(c>d)
return c;
else
return d;
}
static void Main(){
Summition s=null;
Console.Write("Enter First Number"+"\n");
int e=int.Parse(Console.ReadLine());
Console.Write("Enter Second Number"+"\n");
int f=int.Parse(Console.ReadLine());
Console.WriteLine("Please Select Your Choise :");
Console.WriteLine("Enter + for Addition");
Console.WriteLine("Enter - for Subtraction");
Console.WriteLine("Enter M/m to find max number");
char x=char.Parse(Console.ReadLine());
if(x=='+'){
s=new Summition(sum);    //step-3
}
else if(x=='-'){
s=new Summition(diff);    //step-3
}
else{
s=new Summition(max);    //step-3
}
//Console.WriteLine("Please select correct choise");
int res=s(e,f);  //step-4
Console.WriteLine("Result is:"+res);
Console.Write("Plese Enter - Enter Key to Exit");
Console.ReadLine();
}
}
}
Answers (1)