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. Tornado实战项目(伪JD商城)

    预备知识 在之前tornado商城项目中,在开始之前需要引入一些项目设计知识,如接口,抽象方法抽象类,组合,程序设计原则等,个人理解项目的合理设计可增加其灵活性, 降低数据之间的耦合性,提高稳定性,下 ...

  2. C#委托理解(个人观点)

    前言: 根据百度百科字面意思是:把事情托付给别人或别的机构(办理/处理), 我们就按汉字意思来理解; 再罗嗦一点通俗一点就是:当某人发生什么事情后把处理这个事情的工作托付给别人或别的机构(办理/处理) ...

  3. .NET程序集的编译目标平台:X86 &AnyCPU &X64

    在我们测试平台上发布客户端组件,经常会碰到因为build的版本是x86还是anycpu而引起的application error的问题.借此,研究了一下X86,X64,AnyCPU的区别. 使用.ne ...

  4. LR12.53—第3课:重播Vuser脚本

    第3课:重播Vuser脚本 在前面的教训,你记录了一组典型的用户行为,并准备重播脚本. 重播脚本之前,您可以配置脚本的运行时设置,它定义了Vuser的行为. 注:记录与基于Web的协议的地方WebTo ...

  5. input file 图片上传

    使用第三方:jquery.ajaxfileupload.jsinput中的name根据后端来定 <form method="post" enctype="multi ...

  6. C# 调用Adodb对Access数据库执行批量插入

    public void BatchInsertIntoAccess(DataTable dt) { ADODB.Connection cn; ADODB.Recordset rs; string st ...

  7. 关于checkbox全选与反选的问题

    在一组checkbox中常有这样的需求,选择全选按钮,所有的选项必须全选上,当再次点击时,则所有的按钮必须反选,当点击一组checkbox时,只有有一个不选上,则按钮不选中,当所有的按钮全部选上时,此 ...

  8. my sql中join的操作

    SQL标准中的Join的类型:  首先,设置表employees和department的数据为:  1.inner join … on操作类型 内连接inner join是基于连接谓词将两张表(如A和 ...

  9. FFT的分析以及matlab实验

    FFT(Fast Fourier Transformation),即为快速傅氏变换,是离散傅氏变换(DFT)的快速算法. 采样得到的数字信号,做FFT变换,N个采样点,经过FFT之后,就可以得到N个点 ...

  10. 利用循环播放dataurl的视频来防止锁屏:NoSleep.js

    mark下. 地址:http://www.open-open.com/lib/view/open1430796889882.html