委托的应用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 委托的应用
{
//1.声明委托类型

public delegate int CalculatDegate(int a, int b);


class Program
{
public static int Add(int a, int b)
{
return a + b;
}
public static int Sub(int a, int b)
{
return a - b;
}
static void Main(string[] args)
{
//2.创建委托对象,关联具体方法
CalculatDegate calDegate = new CalculatDegate(Add);
calDegate -= Add;//断开当前委托关联方法
calDegate += Sub;//重新调用新的委托关联方法(减法)
//通过委托调用方法
int resulte = calDegate(30, 20);

Console.WriteLine("result: {0}",resulte);
Console.ReadLine();
}
}
}

 

posted @ 2022-05-10 09:34  ~磊  阅读(24)  评论(0编辑  收藏  举报