一 设计界面
二 编写代码
1 委托的第一种方法(声明 实例化 使用 )
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 计算
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate int Calculate(int x, int y); //声明委托——1
int Add(int x, int y) { return x + y; }//加、减、乘、除的方法——2
int Minus(int x, int y) { return x - y; }
int Multiply(int x, int y) { return x * y; }
int Divide(int x, int y) { return x / y; }
private void button1_Click(object sender, EventArgs e)
{
Calculate a = new Calculate(Add); //通过Calculate型的委托对象a来调用方法Add——3
Calculate b = new Calculate(Minus);
Calculate c = new Calculate(Multiply);//3
Calculate d = new Calculate(Divide);
int result1 = a(Convert.ToInt32(textBox1 .Text),Convert .ToInt32(textBox2 .Text)); //返回值赋给变量result1——4
label2.Text = "" + result1; //显示结果——5
int result2 = b(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text));
label2.Text += "\n\n" + result2;
int result3 = c(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text));
label2.Text += "\n\n" + result3;
int result4 = d(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text));
label2.Text += "\n\n" + result4;
}
}
}
2 委托的第二种 第三种方法(匿名方法+Lambda表达式)
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 计算
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate int Calculate(int x, int y); //声明委托——1
public Calculate handler; //定义委托型的字段——2
private void button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(textBox1 .Text);
int b = Convert.ToInt32(textBox2 .Text);
//Lambda表达式—— (1)
handler = (x, y) => x + y; //创建委托对象同时封装方法——3
//匿名方法——(2)
// handler = delegate(int x, int y) { return x + y; };
label2.Text = handler(a, b).ToString();//通过委托对象调用方法——4
handler = (x, y) => x - y;
label2.Text += "\n\n" + handler(a,b);
handler = (x, y) => x * y;
label2.Text += "\n\n" + handler(a, b);
handler = (x, y) => x / y;
label2.Text += "\n\n" + handler(a, b);
}
}
}
3 委托的第三种方法
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 计算
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void Calculate(int x, int y); //声明委托——1
public Calculate handler; //定义委托型的字段——2
public void Add(int x, int y) { label2.Text = (x + y).ToString(); }
public void Mul(int x, int y) { label2.Text += "\n\n" + (x * y); }
public void Sub(int x, int y) { label2.Text += "\n\n" + (x - y); }
public void Div(int x, int y) { label2.Text += "\n\n" + (x / y); }
private void button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(textBox1 .Text);
int b = Convert.ToInt32(textBox2 .Text);
handler = new Calculate(Add); //创建委托对象同时封装方法——3
handler += new Calculate(Sub);
handler += new Calculate(Mul);
handler += new Calculate(Div); //通过委托对象调用方法——4
handler(a, b);
}
}
}
三 运行结果