简单使用委托的过程[一]
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 委托练习1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cal obj = new cal(add);//
int res = obj(10, 20);//
Console.WriteLine("10+20={0}", res);//
obj -= add;//与之前指定的方法解绑
obj += sub;//重新指向一个新的方法
Console.WriteLine("10+20={0}", res);//
Console.ReadLine();
}
static int add(int a, int b)//根据委托定义一个具体的方法(加法功能)
{
return a + b ;
}
static int sub(int a, int b)//根据委托定义一个具体的方法(减法功能)
{
return a - b;
}
public delegate int cal(int a, int b);//
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 委托练习1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cal obj = new cal(add);//
int res = obj(10, 20);//
Console.WriteLine("10+20={0}", res);//
obj -= add;//与之前指定的方法解绑
obj += sub;//重新指向一个新的方法
Console.WriteLine("10+20={0}", res);//
Console.ReadLine();
}
static int add(int a, int b)//根据委托定义一个具体的方法(加法功能)
{
return a + b ;
}
static int sub(int a, int b)//根据委托定义一个具体的方法(减法功能)
{
return a - b;
}
public delegate int cal(int a, int b);//
}
}