1. delegate

example1

class Program
{
public delegate int MyDelegate(int i); int MyFunc(int i)
{
return i;
} public void Foo()
{
MyDelegate f = MyFunc; Console.WriteLine(f());
Console.ReadKey();
}
}

example 2

// declare the delegate type
public delegate string MyDelegate(int arg1, int arg2); class Program
{
// INSERT DELEGATES HERE
static string func1(int a, int b)
{
return (a + b).ToString();
}
static string func2(int a, int b)
{
return (a * b).ToString();
} static void Main(string[] args)
{
MyDelegate f = func1;
Console.WriteLine("The number is: " + f(, ));
f = func2;
Console.WriteLine("The number is: " + f(, )); Console.WriteLine("\nPress Enter Key to Continue...");
Console.ReadLine();
}
}

2. anonymous delegate

 public delegate string MyDelegate(int arg1, int arg2);

    class Program
{
static void Main(string[] args)
{
// SNIPPET GOES HERE
MyDelegate f = delegate(int arg1, int arg2)
{
return (arg1 + arg2).ToString();
};
Console.WriteLine("The number is: " + f(, ));
// Keep the console window open until a key is pressed
Console.WriteLine("\nPress Enter Key to Continue...");
Console.ReadLine();
}
}

3. composable delegate (call multiple delegates with one function call)

 // declare the delegate type
public delegate void MyDelegate(int arg1, int arg2); class Program
{
static void func1(int arg1, int arg2)
{
string result = (arg1 + arg2).ToString();
Console.WriteLine("The number is: " + result);
}
static void func2(int arg1, int arg2)
{
string result = (arg1 * arg2).ToString();
Console.WriteLine("The number is: " + result);
}
static void Main(string[] args)
{
MyDelegate f1 = func1;
MyDelegate f2 = func2;
MyDelegate f1f2 = f1 + f2; // call each delegate and then the chain
Console.WriteLine("Calling the first delegate");
f1(, );
Console.WriteLine("Calling the second delegate");
f2(, );
Console.WriteLine("\nCalling the chained delegates");
f1f2(, ); // subtract off one of the delegates
Console.WriteLine("\nCalling the unchained delegates");
f1f2 -= f1;
f1f2(, ); Console.WriteLine("\nPress Enter Key to Continue...");
Console.ReadLine();
}
}

C# delegate的更多相关文章

  1. [.NET] C# 知识回顾 - 委托 delegate (续)

    C# 知识回顾 - 委托 delegate (续) [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6046171.html 序 上篇<C# 知识回 ...

  2. [C#] C# 知识回顾 - 委托 delegate

    C# 知识回顾 - 委托 delegate [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6031892.html 目录 What's 委托 委托的属性 ...

  3. iOS 键盘添加完成按钮,delegate和block回调

    这个是一个比较初级一点的文章,新人可以看看.当然实现这个需求的时候自己也有一点收获,记下来吧. 前两天产品要求在工程的所有数字键盘弹出时,上面带一个小帽子,上面安装一个“完成”按钮,这个完成按钮也没有 ...

  4. C# 委托Delegate(一) 基础介绍&用法

    本文是根据书本&网络 前人总结的. 1. 前言 定义&介绍: 委托Delegate是一个类,定义了方法的类型, 使得可以将方法当做另一个方法的参数来进行传递,这种将方法动态地赋给参数的 ...

  5. Jquery中的bind(),live(),delegate(),on()绑定事件方式

    博客转载为作者:枫上善若水http://www.cnblogs.com/xilipu31/p/4105794.html 前言 因为项目中经常会有利用jquery操作dom元素的增删操作,所以会涉及到d ...

  6. C#基础知识六之委托(delegate、Action、Func、predicate)

    1. 什么是委托 官方解释 委托是定义方法签名的类型,当实例化委托时,您可以将其实例化与任何具有兼容签名的方法想关联,可以通过委托实例调用方法. 个人理解 委托通俗一点说就是把一件事情交给别人来帮助完 ...

  7. [转载]C#委托和事件(Delegate、Event、EventHandler、EventArgs)

    原文链接:http://blog.csdn.net/zwj7612356/article/details/8272520 14.1.委托 当要把方法作为实参传送给其他方法的形参时,形参需要使用委托.委 ...

  8. jQuery 中bind(),live(),delegate(),on() 区别(转)

    当我们试图绑定一些事件到DOM元素上的时候,我相信上面这4个方法是最常用的.而它们之间到底有什么不同呢?在什么场合下用什么方法是最有效的呢? 准备知识: 当我们在开始的时候,有些知识是必须具备的: D ...

  9. UITableview delegate dataSource调用探究

    UITableview是大家常用的UIKit组件之一,使用中我们最常遇到的就是对delegate和dataSource这两个委托的使用.我们大多数人可能知道当reloadData这个方法被调用时,de ...

  10. C#委托的介绍(delegate、Action、Func、predicate)

    委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 (1). delegate delegate我们常用到的一种声明   Deleg ...

随机推荐

  1. cocos2dx day 1

    原文:http://www.cocos2d-x.org/programmersguide/2/index.html 一.Basic Concepts 1.director 2.scene 2.1 sc ...

  2. Spring注解学习

    参考链接 http://blog.csdn.net/xyh820/article/details/7303330/

  3. Windows MFC 两个OpenGL窗口显示与线程RC问题

    问题为:背景界面是一个OpenGL窗口(对话框),在其上弹出一个OpenGL窗口(模态对话框)时, 1.上方的OpenGL窗口能响应鼠标操作等并刷新: 2.当移动或放大缩小上方的OpenGL窗口时,其 ...

  4. Flex数据交互之Remoting

    一 前言 Flex数据交互常用的有三种方式:WebService.HttpService以及Remoting. WebService方式已在这篇文章中给出,这篇文章主要讲解以Remoting方式进行数 ...

  5. Android狂记忆

    虽然说技术人员偏爱实战,而不屑理论或记忆,但实战之前,若是记忆一些知识,开发起来将会如虎添翼,不说了,开始狂记吧! Android 系统包说明: android.app  :提供高层的程序模型.提供基 ...

  6. Object-C 中各数据类型转换 NSData转NSString,Byte,UIImage

    1,NSData 与 NSString NSData --> NSString NSString *aString = [[NSString alloc] initWithData:adata ...

  7. GCD、dispatch函数介绍

    iOS多线程的方法有3种: NSThread NSOperation GCD(Grand Central Dispatch) 其中,由苹果所倡导的为多核的并行运算提出的解决方案:GCD能够访问线程池, ...

  8. 前端学习实践笔记--JavaScript深入【3】

    这章主要讨论闭包和原型,以及面向对象和继承. 闭包 闭包充分利用了JS里面作用域的概念,作用域的好处是内部函数可以访问定义它们的外部函数的参数和变量.使用闭包主要是为了读取函数内部的变量或者将函数内部 ...

  9. SpringBoot Demo

    Spring Boot,微框架,确实不错,很方便. 相关网站链接: http://www.tuicool.com/articles/veUjQba https://www.gitbook.com/bo ...

  10. go语言环境搭建+sublime text3(windows环境下)

    感觉有点坑,整了一下午~搞定 go语言环境搭建+sublime text3(windows环境下) 1.安装sublime text3 2.安装go语言程序包 3.测试go语言是否安装成功     键 ...