委托、 Lambda表达式和事件——委托

时间:2023-03-09 03:55:26
委托、 Lambda表达式和事件——委托
  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);
    }
    }
    }