public delegate void MyDelegate(string mydelegate);//声明一个delegate对象

//实现有相同参数和返回值的函数
        public void HelloDelegate(string mydelegate)
        {
            Console.WriteLine(mydelegate);
        }

MyDelegate mydelegate = new MyDelegate(testClass.HelloDelegate);//产生delegate对象
mydelegate("Hello delegate");//调用

From MSDN: [C# delegate的演进]

  1. class Test
  2. {
  3. delegate void TestDelegate(string s);
  4. static void M(string s)
  5. {
  6. Console.WriteLine(s);
  7. }
  8.  
  9. static void Main(string[] args)
  10. {
  11. // Original delegate syntax required
  12. // initialization with a named method.
  13. TestDelegate testDelA = new TestDelegate(M);
  14.  
  15. // C# 2.0: A delegate can be initialized with
  16. // inline code, called an "anonymous method.匿名函式" This
  17. // method takes a string as an input parameter.
  18. TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };
  19.  
  20. // C# 3.0. A delegate can be initialized with
  21. // a lambda expression. The lambda also takes a string
  22. // as an input parameter (x). The type of x is inferred by the compiler.
  23. TestDelegate testDelC = (x) => { Console.WriteLine(x); };
  24.  
  25. // Invoke the delegates.
  26. testDelA("Hello. My name is M and I write lines.");
  27. testDelB("That's nothing. I'm anonymous and ");
  28. testDelC("I'm a famous author.");
  29.  
  30. // Keep console window open in debug mode.
  31. Console.WriteLine("Press any key to exit.");
  32. Console.ReadKey();
  33. }
  34. }
  35. /* Output:
  36. Hello. My name is M and I write lines.
  37. That's nothing. I'm anonymous and
  38. I'm a famous author.
  39. Press any key to exit.
  40. */
  41.  
  42. =====Declaring an event=====   
    1, To declare an event inside a class, first a delegate type for the event must be declared, if none is already declared.
  1. public delegate void ChangedEventHandler(object sender, EventArgs e);
  1. 2, Next, the event itself is declared.
  1. public event ChangedEventHandler Changed;
  1. 3, Invoking an event
  1. if (Changed != null)
  2. Changed(this, e);
    4, Hooking up to an event
  • Compose a new delegate onto that field.
  1. // Add "ListChanged" to the Changed event on "List":
  2. List.Changed += new ChangedEventHandler(ListChanged);
  • Remove a delegate from a (possibly composite) field.
  1. // Detach the event and delete the list:
  2. List.Changed -= new ChangedEventHandler(ListChanged);
  1.  

C# delegate & event的更多相关文章

  1. C# delegate event func action 匿名方法 lambda表达式

    delegate event action func 匿名方法 lambda表达式 delegate类似c++的函数指针,但是是类型安全的,可以指向多个函数, public delegate void ...

  2. [UE4] C++实现Delegate Event实例(例子、example、sample)

    转自:http://aigo.iteye.com/blog/2301010 虽然官方doc上说Event的Binding方式跟Multi-Cast用法完全一样,Multi-Cast论坛上也有很多例子, ...

  3. [C#] Delegate, Multicase delegate, Event

    声明:这篇博客翻译自:https://www.codeproject.com/Articles/1061085/Delegates-Multicast-delegates-and-Events-in- ...

  4. Delegate&Event

    Delegate 1.基本类: public class Student { public int Id { get; set; } public string Name { get; set; } ...

  5. Delegate & Event

    Long time without coding,貌似对programming都失去了曾有的一点点sense了,今日有空再细瞄一下.net的委托和事件. Delegate 首先,委托用于引用一类具有相 ...

  6. Delegate event 委托事件---两个From窗体使用委托事件

    窗体如下:   public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void b ...

  7. 委托与事件--delegate&&event

    委托 访问修饰符 delegate 返回值 委托名(参数); public delegate void NoReturnNoPara(); public void NoReturnNoParaMeth ...

  8. delegate, event - 里面涉及的参数类型必须完全一致,子类是不行的

    public void TestF() { Test += Fun; } public void Fun(Person p) { }  // 如 Person变成 SubPerson,则报错..pub ...

  9. ue4 delegate event

    官网相关 https://docs.unrealengine.com/latest/CHN/Programming/UnrealArchitecture/Delegates/index.html wi ...

随机推荐

  1. Qt之QPauseAnimation

    简述 QPauseAnimation类为QSequentialAnimationGroup提供了一个暂停. 如果你想为QSequentialAnimationGroup动画之间添加延迟,可以插入一个Q ...

  2. IO流 总结二

    流只能操作数据. File 类 用来将文件或者文件夹封装成对象. 方便文件与文件夹进行操作 File对象可以作为参数传递给流的构造函数. 可以将已有的和已出现的文件或者文件夹封装成对象 File a ...

  3. iOS开发之——从零开始完成页面切换形变动画

    前言 某天我接到了UI发给我的两张图: 需求图.png 看到图的时候我一脸懵逼,显然我需要做一个页面切换的指示动画.老实说,从大三暑假开始做iOS开发也一年有余了,但是遇到复杂动画总是唯恐避之不及,只 ...

  4. LayoutInflater和inflate()方法的用法

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 实现LayoutInflater的实例化共有3种方法, (1).通过SystemService获得 Layou ...

  5. Java 集合系列 17 TreeSet

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  6. 怎么实现form表单提交后不重新刷新当前页面

    怎么实现表单提交后不重新刷新当前页面     如何实现表单提交后不重新刷新当前页面 <form name='form1' id='form1' action='/xbcw/cw/xx_xx.ac ...

  7. oAuth协议学习

    我们的项目需要为一个认证网站开发一套API,这些API可以提供给很多公司来调用,但是公司在调用之前,必须先做授权认证,由此接触到了oAuth协议. 以下内容来自网络整理 定义 OAUTH协议为用户资源 ...

  8. c# form的设定

    1. 窗体的显示位置startPosition CenterScreen 窗体在当前居中 CenterParent 窗体在其父窗体居中 WindowDefaultBounds 窗体定期在windows ...

  9. 216. Combination Sum III——本质DFS

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  10. MVC1-5直接访问静态页面

    MVC模式下默认是无法访问站点内静态页面,昨日百度找了半天试了半天才试成功. 默认在Views文件外的静态页面可以访问,若要访问Views里的静态页面则需要修改View文件夹中的web.config: ...