using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; /* //由框架提供的委托类型
//无返的
Action act0 = () => Console.WriteLine("无参无返");
Action<int> act1 = x => Console.WriteLine("1 参无返");
Action<int, int> act2 = (x, y) => Console.WriteLine("2 参无返");
//有返的
Func<string> func0 = () => "无参有返";
Func<int, string> func1 = x => "1 参有返";
Func<int, double, string> func2 = (x, y) => "多参有返"; */ namespace 委托_deleget
{ // 1、声明委托
//就声明方法模版加 deleget 修饰
//就是把方法转义成类,同时指定这个类的使用规则,如 返、参的类型或数量 public delegate void nRnC();
public delegate int hRnC();
delegate string hRhC(string a, int b); class Program
{
// 2、定义供委托调用的方法
private static void for_nRnC_1() { Console.WriteLine("for_nRnC_1"); }
private static void for_nRnC_2() { Console.WriteLine("for_nRnC_2"); }
private static string for_hRhC(string a, int v)
{
string temp = null;
for (int i = 0; i < v; i++)
{
temp += a;
}
return temp;
} static void Main(string[] args)
{
// 3、实例化委托对象,它包括一个调用方法列表 //无参无返
nRnC myDeleget_nRnC = new nRnC(for_nRnC_1);
//有参有返,
hRhC myDeleget_hRhC = new hRhC(for_hRhC); // 4、使用(执行)委托 // nRnC
myDeleget_nRnC.Invoke(); //hRhC
string ret = myDeleget_hRhC("batu", 5);
Console.WriteLine(ret); //多波委托,返回值为最后一个方法的返回值
myDeleget_nRnC += for_nRnC_2;
myDeleget_nRnC -= for_nRnC_2; Console.Read();
}
} //事件_Event class EventTest
{
void doSomeThing()
{
ForEvent myTest = new ForEvent();
myTest.shijian += MyTest_shijian;
//myTest.shijian = MyTest_shijian;
//myTest.shijian();
myTest.weituo = MyTest_shijian;
myTest.weituo();
} private void MyTest_shijian() {; ; ; }
} class ForEvent
{
//这是一个属性,可以直接赋值和执行
//相当于把类内部的方法直接暴露出来
public nRnC weituo { set; get; } //这是一个事件,只能 += 或 -= 且不能从外部调用
//Event 的用作是,控制对象的权限,
//在外部只能向这个事件上挂方法或删方法,不能执行,也不能改变原有方法
public event nRnC shijian;
}
}

  

最新文章

  1. 深入浅出Java三大框架SSH与MVC的设计模式
  2. 【原创】.NET Core应用类型(Portable apps &amp; Self-contained apps)
  3. sdutoj 2154 Shopping
  4. Unity3d 适配机型
  5. Java 数组操作
  6. POJ 1986 Distance Queries (最近公共祖先,tarjan)
  7. AVL树的插入操作(旋转)图解
  8. liger 的 ligerDialog 的使用。
  9. jquery处理textarea中的手动换行
  10. 那些年被我坑过的Python——你来我往(第九章 selectors)
  11. react-native 布局基础
  12. HDU4738【杭州网赛、判桥】
  13. lighttpd配置虚拟主机/php等WEB环境
  14. SIM卡厂商的识别方法
  15. C语言 第三次作业--函数
  16. App专项测试之弱网测试
  17. Fedora 19安装mysql
  18. update 中实现子查询
  19. CF1064A 【Make a triangle!】
  20. POJ - 1054 The Troublesome Frog 模拟 枚举优化。

热门文章

  1. Delphi 日期函数列表
  2. delphi DBGrid简单自绘(字体颜色、背景等)
  3. mysql 删除同样记录只保留一条
  4. PHP mysqli_affected_rows() 函数
  5. 线性筛积性函数+反演T套路——bzoj4407
  6. 大转盘抽奖css3+js(简单书写)
  7. Delphi获取句柄
  8. 用pytorch进行CIFAR-10数据集分类
  9. Linux_磁盘分区、挂载、查看
  10. Function(高阶函数式编程)