1. 简单示例

     /*
    * 由SharpDevelop创建。
    * 用户: David Huang
    * 日期: 2015/7/27
    * 时间: 10:22
    */
    using System; namespace Delegate_Book
    {
    class Program
    {
    delegate string GetAString(); struct Currency
    {
    public uint Dollars;
    public ushort Cents; public Currency(uint Dollars,ushort Cents)
    {
    this.Dollars=Dollars;
    this.Cents=Cents;
    } public override string ToString()
    {
    return string.Format("${0}.{1,2:00}",Dollars,Cents);
    } public static string GetCurrencyUnit()
    {
    return "Dollar";
    }
    } public static void Main(string[] args)
    {
    const int x = ;
    //GetAString getAString =new GetAString(x.ToString);
    GetAString getAString=x.ToString;
    Console.WriteLine(getAString()); Currency currency=new Currency(,);
    getAString=currency.ToString;
    Console.WriteLine(getAString()); Console.WriteLine(new GetAString(new Currency(,).ToString)()); getAString=Currency.GetCurrencyUnit;
    Console.WriteLine(getAString()); Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
    }
    }
    }
  2. Action<T>和Func<T>委 托
     /*
    * 由SharpDevelop创建。
    * 用户: David Huang
    * 日期: 2015/7/28
    * 时间: 14:31
    */
    using System; namespace MathOperations
    {
    static class MathOperations
    {
    public static double MutiplyByTwo(double value)
    {
    return *value;
    } public static double Square(double value)
    {
    return value*value;
    }
    } delegate double DoubleOp(double x); class Program
    { public static void Main(string[] args)
    {
    DoubleOp[] doubleOps={
    MathOperations.MutiplyByTwo,
    MathOperations.Square
    }; foreach (var op in doubleOps) {
    DoTheMath(op,2.5);
    DoTheMath(op,3.5);
    } Func<double,double>[] doubleFuncs={
    MathOperations.MutiplyByTwo,
    MathOperations.Square
    }; foreach (var func in doubleFuncs) {
    DoTheFunc(func,2.5);
    DoTheFunc(func,3.5);
    } Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
    } static void DoTheMath(DoubleOp operation,double value)
    {
    double result=operation(value);
    Console.WriteLine(string.Format("vlaue is {0},result is {1}",value,result));
    } static void DoTheFunc(Func<double,double> func,double value)
    {
    var result = func(value);
    Console.WriteLine(string.Format("value is {0},result is {1}",value,result));
    } }
    }
  3. 冒泡排序示例
     /*
    * 由SharpDevelop创建。
    * 用户: David Huang
    * 日期: 2015/7/29
    * 时间: 15:34
    */
    using System;
    using System.Linq;
    using System.Collections.Generic; namespace BubbleSorter
    {
    class Program
    {
    public static void Main(string[] args)
    {
    //int
    List<int> array=new List<int> {,,,,};
    Sort(array);
    Console.WriteLine(array.ConvertAll(i=>i.ToString()).Aggregate((current,next)=>string.Format("{0},{1}",current,next)).TrimEnd(',')); //泛型
    List<int> list=new List<int> {,,,,};
    Sort(list,IsBiggerThan);
    Console.WriteLine(array.ConvertAll(i=>i.ToString()).Aggregate((current,next)=>string.Format("{0},{1}",current,next)).TrimEnd(',')); //泛型
    List<Person> personList=new List<Person>{
    new Person{Name="Tom",Age=},
    new Person{Name="Jack",Age=},
    new Person{Name="Penny",Age=},
    new Person{Name="Lucy",Age=}
    };
    Sort(personList,IsOlderThan);
    foreach (Person person in personList) {
    Console.WriteLine(person);
    } Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
    } static bool IsOlderThan(Person p1,Person p2)
    {
    return p1.Age>p2.Age;
    } static bool IsBiggerThan(int a,int b)
    {
    return a>b;
    } static void Sort(IList<int> sortArray)
    {
    bool swapped=true; do
    {
    swapped=false; for (int i = ; i < sortArray.Count-; i++) {
    if (sortArray[i]>sortArray[i+]) {
    int tmp=sortArray[i];
    sortArray[i]=sortArray[i+];
    sortArray[i+]=tmp;
    swapped=true;
    }
    } }while (swapped);
    } static void Sort<T>(IList<T> list,Func<T,T,bool> comparison)
    {
    bool swapped=true; do{
    swapped=false; for (int i = ; i < list.Count-; i++) {
    if (comparison(list[i],list[i+])) {
    T temp=list[i];
    list[i]=list[i+];
    list[i+]=temp;
    swapped=true;
    }
    } }while(swapped);
    }
    } class Person
    {
    public string Name{get;set;}
    public int Age{get;set;} public override string ToString()
    {
    return string.Format("Name:{0},Age:{1}",Name,Age);
    }
    }
    }
  4. 多播委托
     /*
    * 由SharpDevelop创建。
    * 用户: David Huang
    * 日期: 2015/7/30
    * 时间: 14:37
    */
    using System; namespace 多播
    {
    public static class MathOperations
    {
    public static void MutiplyByTwo(double value)
    {
    Console.WriteLine(string.Format("method: MutiplyByTwo, value: {0}, result: {1}",value,*value));
    } public static void Square(double value)
    {
    Console.WriteLine(string.Format("method: Square, value: {0}, result: {1}",value,value*value));
    }
    } class Program
    {
    public static void Main(string[] args)
    {
    Action<double> mathOperations=MathOperations.MutiplyByTwo;
    mathOperations+=MathOperations.Square; DoTheMath(mathOperations,2.5);
    DoTheMath(mathOperations,3.5); Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
    } static void DoTheMath(Action<double> action,double value)
    {
    action(value);
    }
    }
    }
  5. 匿名委托
     /*
    * 由SharpDevelop创建。
    * 用户: David Huang
    * 日期: 2015/7/30
    * 时间: 16:19
    */
    using System; namespace 匿名
    {
    class Program
    {
    public static void Main(string[] args)
    {
    Func<int,int> anonDel=delegate(int i)
    {
    return i*;
    }; Console.WriteLine(anonDel());
    // TODO: Implement Functionality Here Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
    }
    }
    }

委托、 Lambda表达式和事件——委托的更多相关文章

  1. C#编程 委托 Lambda表达式和事件

    委托 如果我们要把方法当做参数来传递的话,就要用到委托.简单来说委托是一个类型,这个类型可以赋值一个方法的引用. 声明委托 在C#中使用一个类分两个阶段,首选定义这个类,告诉编译器这个类由什么字段和方 ...

  2. C#学习笔记三(委托·lambda表达式和事件,字符串和正则表达式,集合,特殊的集合)

    委托和事件的区别 序号 区别 委托 事件 1 是否可以使用=来赋值 是 否 2 是否可以在类外部进行调用 是 否 3 是否是一个类型 是 否,事件修饰的是一个对象 public delegate vo ...

  3. 委托、Lambda表达式、事件系列07,使用EventHandler委托

    谈到事件注册,EventHandler是最常用的. EventHandler是一个委托,接收2个形参.sender是指事件的发起者,e代表事件参数. □ 使用EventHandler实现猜拳游戏 使用 ...

  4. 委托、Lambda表达式、事件系列06,使用Action实现观察者模式,体验委托和事件的区别

    在"实现观察者模式(Observer Pattern)的2种方式"中,曾经通过接口的方式.委托与事件的方式实现过观察者模式.本篇体验使用Action实现此模式,并从中体验委托与事件 ...

  5. 委托、Lambda表达式、事件系列05,Action委托与闭包

    来看使用Action委托的一个实例: static void Main(string[] args) { int i = 0; Action a = () => i++; a(); a(); C ...

  6. 委托、Lambda表达式、事件系列04,委托链是怎样形成的, 多播委托, 调用委托链方法,委托链异常处理

    委托是多播委托,我们可以通过"+="把多个方法赋给委托变量,这样就形成了一个委托链.本篇的话题包括:委托链是怎样形成的,如何调用委托链方法,以及委托链异常处理. □ 调用返回类型为 ...

  7. 委托、Lambda表达式、事件系列03,从委托到Lamda表达式

    在"委托.Lambda表达式.事件系列02,什么时候该用委托"一文中,使用委托让代码简洁了不少. namespace ConsoleApplication2 { internal ...

  8. 委托、Lambda表达式、事件系列02,什么时候该用委托

    假设要找出整型集合中小于5的数. static void Main(string[] args) { IEnumerable<int> source = new List<int&g ...

  9. 委托、Lambda表达式、事件系列01,委托是什么,委托的基本用法,委托的Method和Target属性

    委托是一个类. namespace ConsoleApplication1 { internal delegate void MyDelegate(int val); class Program { ...

随机推荐

  1. 【POJ3208】 (DP)

    Apocalypse Someday Description The number 666 is considered to be the occult “number of the beast” a ...

  2. 李洪强iOS开发之-环信01_iOS SDK 前的准备工作

    李洪强iOS开发之-环信01_iOS SDK 前的准备工作 1.1_注册环信开发者账号并创建后台应用 详细步骤:  注册并创建应用 注册环信开发者账号 第 1 步:在环信官网上点击“即时通讯云”,并点 ...

  3. mysql优化21条经验(转)

    今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序 员需要去关注的事情.当我们去设计数据库表结构,对操作数 ...

  4. 14.8.3 Physical Row Structure of InnoDB Tables InnoDB 表的物理行结构

    14.8.3 Physical Row Structure of InnoDB Tables InnoDB 表的物理行结构 一个InnoDB 表的物理行结构取决于在创建表指定的行格式 默认, Inno ...

  5. shell 执行jar 的命令

    #!/bin/sh ############## #判断是否程序已启动 jappname='Test' mainclasspath="com.company.commontest.test& ...

  6. [PHP] 跳转以及回到原来的地址

    回到原来的地址: 1.PHP(PHP代码) Header('Location:'.$_SERVER["HTTP_REFERER"]); 2.JavaScript(相当于后退按钮,- ...

  7. 双11不再孤单,结识ECharts---强大的常用图表库

    又是一年双十一,广大单身狗们有没有很寂寞(好把,其实我也是)!但是这次的双十一,我不再孤单,因为结识了一个js的强大的图表库---ECharts. 最近做软件工程项目的时候,由于设计图中有柱状图和饼图 ...

  8. 神器 Sublime Text 3 的一些常用快捷键

    选择类   Ctrl+D 选中光标所占的文本,继续操作则会选中下一个相同的文本. Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个例子:快速选中并更改所有相同的变量名. ...

  9. UVA 11021 Tribles(递推+概率)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33059 [思路] 递推+概率. 设f[i]表示一只Tribble经 ...

  10. 常用google产品

      常用google产品 01.谷歌阅读器(Google Reader):网页版RSS阅读器,方便订阅,组织和分享新闻.有手机版. 02.谷歌相册服务(Google Picasa):提供照片的下载和编 ...