Code of Delegate Example in C# in windows based application
using System;
using
System.Collections.Generic
using
System.ComponentModel;
using System.Data;
using
System.Drawing;
using System.Linq;
using System.Text;
using
System.Windows.Forms;
namespace
DelegateExample
{
public partial class Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
delegate
double process(double a, double b);
static double Multiply(double
a, double b)
{
return
a * b;
}
static double divide(double
a, double b)
{
return
a / b;
}
private
void comboBox1_SelectedIndexChanged(object sender, EventArgs
e)
{
double
a = Convert.ToDouble(textBox1.Text);
double
b = Convert.ToDouble(textBox2.Text);
process
obj;
if
(comboBox1.SelectedIndex == 0)
{
obj = new
process(Multiply);
}
else
{
obj = new
process(divide);
}
MessageBox.Show("Result :" + Convert.ToString(obj(a,
b)));
}
}
}
No comments:
Post a Comment