C# delegate & event
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的演进]
- class Test
- {
- delegate void TestDelegate(string s);
- static void M(string s)
- {
- Console.WriteLine(s);
- }
- static void Main(string[] args)
- {
- // Original delegate syntax required
- // initialization with a named method.
- TestDelegate testDelA = new TestDelegate(M);
- // C# 2.0: A delegate can be initialized with
- // inline code, called an "anonymous method.匿名函式" This
- // method takes a string as an input parameter.
- TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };
- // C# 3.0. A delegate can be initialized with
- // a lambda expression. The lambda also takes a string
- // as an input parameter (x). The type of x is inferred by the compiler.
- TestDelegate testDelC = (x) => { Console.WriteLine(x); };
- // Invoke the delegates.
- testDelA("Hello. My name is M and I write lines.");
- testDelB("That's nothing. I'm anonymous and ");
- testDelC("I'm a famous author.");
- // Keep console window open in debug mode.
- Console.WriteLine("Press any key to exit.");
- Console.ReadKey();
- }
- }
- /* Output:
- Hello. My name is M and I write lines.
- That's nothing. I'm anonymous and
- I'm a famous author.
- Press any key to exit.
- */
- =====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.
- public delegate void ChangedEventHandler(object sender, EventArgs e);
- 2, Next, the event itself is declared.
- public event ChangedEventHandler Changed;
- 3, Invoking an event
- if (Changed != null)
- Changed(this, e);
4, Hooking up to an event
- Compose a new delegate onto that field.
- // Add "ListChanged" to the Changed event on "List":
- List.Changed += new ChangedEventHandler(ListChanged);
- Remove a delegate from a (possibly composite) field.
- // Detach the event and delete the list:
- List.Changed -= new ChangedEventHandler(ListChanged);
C# delegate & event的更多相关文章
- C# delegate event func action 匿名方法 lambda表达式
delegate event action func 匿名方法 lambda表达式 delegate类似c++的函数指针,但是是类型安全的,可以指向多个函数, public delegate void ...
- [UE4] C++实现Delegate Event实例(例子、example、sample)
转自:http://aigo.iteye.com/blog/2301010 虽然官方doc上说Event的Binding方式跟Multi-Cast用法完全一样,Multi-Cast论坛上也有很多例子, ...
- [C#] Delegate, Multicase delegate, Event
声明:这篇博客翻译自:https://www.codeproject.com/Articles/1061085/Delegates-Multicast-delegates-and-Events-in- ...
- Delegate&Event
Delegate 1.基本类: public class Student { public int Id { get; set; } public string Name { get; set; } ...
- Delegate & Event
Long time without coding,貌似对programming都失去了曾有的一点点sense了,今日有空再细瞄一下.net的委托和事件. Delegate 首先,委托用于引用一类具有相 ...
- Delegate event 委托事件---两个From窗体使用委托事件
窗体如下: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void b ...
- 委托与事件--delegate&&event
委托 访问修饰符 delegate 返回值 委托名(参数); public delegate void NoReturnNoPara(); public void NoReturnNoParaMeth ...
- delegate, event - 里面涉及的参数类型必须完全一致,子类是不行的
public void TestF() { Test += Fun; } public void Fun(Person p) { } // 如 Person变成 SubPerson,则报错..pub ...
- ue4 delegate event
官网相关 https://docs.unrealengine.com/latest/CHN/Programming/UnrealArchitecture/Delegates/index.html wi ...
随机推荐
- Qt之QPauseAnimation
简述 QPauseAnimation类为QSequentialAnimationGroup提供了一个暂停. 如果你想为QSequentialAnimationGroup动画之间添加延迟,可以插入一个Q ...
- IO流 总结二
流只能操作数据. File 类 用来将文件或者文件夹封装成对象. 方便文件与文件夹进行操作 File对象可以作为参数传递给流的构造函数. 可以将已有的和已出现的文件或者文件夹封装成对象 File a ...
- iOS开发之——从零开始完成页面切换形变动画
前言 某天我接到了UI发给我的两张图: 需求图.png 看到图的时候我一脸懵逼,显然我需要做一个页面切换的指示动画.老实说,从大三暑假开始做iOS开发也一年有余了,但是遇到复杂动画总是唯恐避之不及,只 ...
- LayoutInflater和inflate()方法的用法
LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 实现LayoutInflater的实例化共有3种方法, (1).通过SystemService获得 Layou ...
- Java 集合系列 17 TreeSet
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- 怎么实现form表单提交后不重新刷新当前页面
怎么实现表单提交后不重新刷新当前页面 如何实现表单提交后不重新刷新当前页面 <form name='form1' id='form1' action='/xbcw/cw/xx_xx.ac ...
- oAuth协议学习
我们的项目需要为一个认证网站开发一套API,这些API可以提供给很多公司来调用,但是公司在调用之前,必须先做授权认证,由此接触到了oAuth协议. 以下内容来自网络整理 定义 OAUTH协议为用户资源 ...
- c# form的设定
1. 窗体的显示位置startPosition CenterScreen 窗体在当前居中 CenterParent 窗体在其父窗体居中 WindowDefaultBounds 窗体定期在windows ...
- 216. Combination Sum III——本质DFS
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- MVC1-5直接访问静态页面
MVC模式下默认是无法访问站点内静态页面,昨日百度找了半天试了半天才试成功. 默认在Views文件外的静态页面可以访问,若要访问Views里的静态页面则需要修改View文件夹中的web.config: ...