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. 一键配置openvpn

    页面:https://github.com/Nyr/openvpn-install openvpn-install OpenVPN road warrior installer for Debian, ...

  2. MySQL For Windows修改最大连接数

    1.从官网下载安装MySQL Installer.MySQL Installer 提供了简单易用.向导式的 MySQL 软件的安装体验过程(目前只支持 Windows),包含的产品有: MySQL S ...

  3. w3svc服务启动 不了,错误 1068:依赖服务或组件无法启动

    win10系统,装了iis就是启动不了,报错误 1068:依赖服务或组件无法启动. 各种实验无法使用,最后如下方法解决 运行命令regedit,打开注册表编辑器,进入:HKEY_LOCAL_MACHI ...

  4. Hive On Spark环境搭建

    Spark源码编译与环境搭建 Note that you must have a version of Spark which does not include the Hive jars; Spar ...

  5. TENDA-F322路由器管理工具

    https://yunpan.cn/cYsfNxJLfVnUY (提取码:d0ae)

  6. 使用Plsql将Excel数据导入Oracle数据库

    1.在plsql页面,Tools-->ODBC Importer... 2.配置被导入的excel files 3.选择excel文件 4.选择Excel中要被导入的的sheet工作簿,选择之后 ...

  7. 在js中怎么样选择互斥的相邻元素

    在使用jquery中,我们通常会选择siblings()去选择相邻元素,使用eq()方法去匹配元素,使用index()获取对应元素的索引值,具体jquery代码如下: <style> *{ ...

  8. DEM数据如何生成高程点

    这次给大家介绍一个arcgis里的实用功能:通过地形数据提取高程点. 首先做好准备工作: 1.地形数据下载获取 2.软件准备 locaspace viewer:http://rj.baidu.com/ ...

  9. HTML的FormData对象

    FormData是HTML5标准制定的一个新的封装对象,有了FormData对象,可以方便地通过javascript创建表单数据,直接以ajax发送到服务器中. 目前浏览器的支持情况为: 详细的介绍与 ...

  10. Programming Entity Framework 翻译(1)-目录

    1. Introducing the ADO.NET Entity Framework ado.net entity framework 介绍 1 The Entity Relationship Mo ...