委托(Delegate)
来源:互联网 发布:windows系统哪个开机快 编辑:程序博客网 时间:2024/06/05 14:45
委声明定义了一种类型,它用一组特定的参数以及类型来封装方法。对于静态方法,委托对象封装要调用的方法,对于实例方法,委托对象同时封装一个实例和该实例上的一个方法。如果您有一个委托对象和一组适当的参数,则可以用这些参数调用该方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 委托1
{
delegate void EatDelegate(string food);
class MyDelegate
{
//以下为委托形成原理代码
//static void zsEat(string food)
//{
// Console.WriteLine("张三吃" + food);
//}
//static void lsEat(string food)
//{
// Console.WriteLine("李四吃" + food);
//}
//static void wwEat(string food)
//{
// Console.WriteLine("王五吃" + food);
//}
//static void Main(string[] args)
//{
// EatDelegate zs = new EatDelegate(zsEat);
// EatDelegate ls = new EatDelegate(lsEat);
// EatDelegate ww = new EatDelegate(wwEat);
// zsEat("西瓜");
// lsEat("西瓜");
// wwEat("西瓜");
// EatDelegate EatChain = null;
// EatChain = zs + ls + ww;
// Console.WriteLine("张三,李四,王五一起开Party!");
// EatChain("西瓜");
// Console.WriteLine("张三出去接电话!");
// EatChain -= zs;
// EatChain("桔子");
// Console.WriteLine("张三回来了!");
// EatChain += zs;
// EatChain("香蕉");
//}
//以下为带匿名方法的委托形成原理代码
static void Main(string[] args)
{
EatDelegate EatChain=null;
EatChain += delegate(string food) { Console.WriteLine("张三吃" + food); };//带匿名方法的委托,c#2.0版本开始出现
EatChain += delegate(string food) { Console.WriteLine("李四吃" + food); };//带匿名方法的委托,c#2.0版本开始出现
EatChain += delegate(string food) { Console.WriteLine("王五吃" + food); };//带匿名方法的委托,c#2.0版本开始出现
EatChain("西瓜");
}
}
}
- 委托(Delegate)
- 委托(Delegate)
- 委托(Delegate)
- 委托(Delegate)
- 委托(Delegate)
- 委托(Delegate)
- 委托(Delegate)
- 什么是委托(Delegate)
- 什么是委托(Delegate)
- 委托(Delegate)机制
- 委托(delegate)模式
- 委托(delegate)
- 委托(Delegate)
- 委托(Delegate)简介
- C# 委托(Delegate)
- C# 委托(Delegate)
- C# 委托(Delegate)
- C#委托(delegate)
- 在2.x版本中,将hadoop源代码导入到eclipse中:
- 斯坦福大学机器学习——K-means算法
- 个人作品展示与软件下载
- Sort Colors - Java
- PNG使用技巧。
- 委托(Delegate)
- 数据库连接池C3P0学习
- jQuery中ready与load事件的区别
- mongodb 安装卸载
- Java学习路线图,专为新手定制的Java学习计划建议
- epoll 汇总
- 《大数据日知录:架构与算法》前言
- 嵌入式设备web服务器比较
- C++ Reflection 的简单实现 (反射)