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的更多相关文章

  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. C语言中static变量详解

    Static翻译出来是“静态”“静止”的意思,在C语言中的意思其实和它的本意差不多,表示“静态”或者“全局”的意思,用来修饰变量和函数.经static修饰过后的变量或者函数的作用域或者存储域会发生变化 ...

  2. Eclipse上安装GIT插件EGit

    一.Eclipse上安装GIT插件EGit Eclipse的版本eclipse-java-helios-SR2-win32.zip(在Eclipse3.3版本找不到对应的 EGit插件,无法安装) E ...

  3. Mybatis update In

    mysql语句如下: ,,) mybatis的mapper如下: int updateStateByIDs(@Param("ids") String[] ids, @Param(& ...

  4. 用HTML5实现的各种排序算法的动画比较 及算法小结

    用HTML5实现的各种排序算法的动画比较 http://www.webhek.com/misc/comparison-sort/ 几种排序算法效率的比较 来源:http://blog.chinauni ...

  5. matlab(函数,变量)

  6. Mysql执行Update操作时会锁住表

    update tableA a,(select a.netbar_id,sum(a.reward_amt) reward_amt from tableB a group by a.netbar_id) ...

  7. guava学习--Optional可空类型

    转载:http://www.cnblogs.com/whitewolf/p/4231783.html Null sucks 回到本文主题Optional.在我日常编程中NullPointerExcep ...

  8. ext DateTime.js在ie下显示不全

    问题: ext在使用DateTime.js的时候会出现在日期控件在ie下显示不完成.如图  少了底部的“今天按钮”. 解决方法: 在ext/ux/form/DateTime.js (我的是这个路径,根 ...

  9. springmvc 配置直接访问页面

    <mvc:view-controller path="/" view-name="/home"/> 在mvc中配置,访问路径就可以了

  10. ruby开源项目之Octopress:像黑客一样写博客(zhuan)

    ruby开源项目之Octopress:像黑客一样写博客 百度权重查询 词库网 网站监控 服务器监控 SEO监控 Swift编程语言教程 今年一直推荐的一种写作方式.markdown语法快速成文,git ...