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

  1. 简单示例
     1 /*
     2  * 由SharpDevelop创建。
     3  * 用户: David Huang
     4  * 日期: 2015/7/27
     5  * 时间: 10:22
     6  */
     7 using System;
     8 
     9 namespace Delegate_Book
    10 {
    11     class Program
    12     {
    13         delegate string GetAString();
    14         
    15         struct Currency
    16         {
    17             public uint Dollars;
    18             public ushort Cents;
    19             
    20             public Currency(uint Dollars,ushort Cents)
    21             {
    22                 this.Dollars=Dollars;
    23                 this.Cents=Cents;
    24             }
    25             
    26             public override string ToString()
    27             {
    28                 return string.Format("${0}.{1,2:00}",Dollars,Cents);
    29             } 
    30             
    31             public static string GetCurrencyUnit()
    32             {
    33                 return "Dollar";
    34             }
    35         }
    36         
    37         public static void Main(string[] args)
    38         {
    39             const int x = 5;
    40             //GetAString getAString =new GetAString(x.ToString);
    41             GetAString getAString=x.ToString;
    42             Console.WriteLine(getAString());     
    43             
    44             Currency currency=new Currency(50,60);
    45             getAString=currency.ToString;
    46             Console.WriteLine(getAString());
    47             
    48             Console.WriteLine(new GetAString(new Currency(50,50).ToString)());
    49             
    50             getAString=Currency.GetCurrencyUnit;
    51             Console.WriteLine(getAString());
    52             
    53             
    54             Console.Write("Press any key to continue . . . ");
    55             Console.ReadKey(true);
    56         }
    57     }
    58 }
  2. Action<T>和Func<T>委 托
     1 /*
     2  * 由SharpDevelop创建。
     3  * 用户: David Huang
     4  * 日期: 2015/7/28
     5  * 时间: 14:31
     6  */
     7 using System;
     8 
     9 namespace MathOperations
    10 {
    11     static class MathOperations
    12     {
    13         public static double MutiplyByTwo(double value)
    14         {
    15             return 2*value;
    16         }
    17         
    18         public static double Square(double value)
    19         {
    20             return value*value;
    21         }
    22     }
    23     
    24     delegate double DoubleOp(double x);
    25     
    26     class Program
    27     {
    28         
    29         public static void Main(string[] args)
    30         {
    31             DoubleOp[] doubleOps={
    32                 MathOperations.MutiplyByTwo,
    33                 MathOperations.Square
    34             };
    35             
    36             foreach (var op in doubleOps) {
    37                 DoTheMath(op,2.5);
    38                 DoTheMath(op,3.5);
    39             }
    40             
    41             Func<double,double>[] doubleFuncs={
    42                 MathOperations.MutiplyByTwo,
    43                 MathOperations.Square
    44             };
    45             
    46             foreach (var func in doubleFuncs) {
    47                 DoTheFunc(func,2.5);
    48                 DoTheFunc(func,3.5);
    49             }
    50             
    51             Console.Write("Press any key to continue . . . ");
    52             Console.ReadKey(true);
    53         }
    54         
    55         static void DoTheMath(DoubleOp operation,double value)
    56         {
    57             double result=operation(value);
    58             Console.WriteLine(string.Format("vlaue is {0},result is {1}",value,result));
    59         }
    60         
    61         static void DoTheFunc(Func<double,double> func,double value)
    62         {
    63             var result = func(value);
    64             Console.WriteLine(string.Format("value is {0},result is {1}",value,result));
    65         }
    66         
    67     }
    68 }
  3. 冒泡排序示例
      1 /*
      2  * 由SharpDevelop创建。
      3  * 用户: David Huang
      4  * 日期: 2015/7/29
      5  * 时间: 15:34
      6  */
      7 using System;
      8 using System.Linq;
      9 using System.Collections.Generic;
     10 
     11 namespace BubbleSorter
     12 {
     13     class Program
     14     {
     15         public static void Main(string[] args)
     16         {
     17             //int
     18             List<int> array=new List<int> {5,2,4,6,1};
     19             Sort(array);            
     20             Console.WriteLine(array.ConvertAll(i=>i.ToString()).Aggregate((current,next)=>string.Format("{0},{1}",current,next)).TrimEnd(','));
     21             
     22             //泛型
     23             List<int> list=new List<int> {5,2,4,6,1};
     24             Sort(list,IsBiggerThan);
     25             Console.WriteLine(array.ConvertAll(i=>i.ToString()).Aggregate((current,next)=>string.Format("{0},{1}",current,next)).TrimEnd(','));
     26             
     27             //泛型
     28             List<Person> personList=new List<Person>{
     29                 new Person{Name="Tom",Age=4},
     30                 new Person{Name="Jack",Age=2},
     31                 new Person{Name="Penny",Age=3},
     32                 new Person{Name="Lucy",Age=5}
     33             };
     34             Sort(personList,IsOlderThan);
     35             foreach (Person person in personList) {
     36                 Console.WriteLine(person);
     37             }
     38                        
     39             
     40             Console.Write("Press any key to continue . . . ");
     41             Console.ReadKey(true);
     42         }
     43         
     44         static bool IsOlderThan(Person p1,Person p2)
     45         {
     46             return p1.Age>p2.Age;
     47         }
     48         
     49         static bool IsBiggerThan(int a,int b)
     50         {
     51             return a>b;
     52         }
     53         
     54         static void Sort(IList<int> sortArray)
     55         {
     56             bool swapped=true;
     57             
     58             do
     59             {
     60                 swapped=false;
     61                 
     62                 for (int i = 0; i < sortArray.Count-1; i++) {
     63                     if (sortArray[i]>sortArray[i+1]) {
     64                         int tmp=sortArray[i];
     65                         sortArray[i]=sortArray[i+1];
     66                         sortArray[i+1]=tmp;
     67                         swapped=true;
     68                     }
     69                 }
     70                 
     71             }while (swapped);
     72         }
     73         
     74         static void Sort<T>(IList<T> list,Func<T,T,bool> comparison)
     75         {
     76             bool swapped=true;
     77             
     78             do{
     79                 swapped=false;
     80                 
     81                 for (int i = 0; i < list.Count-1; i++) {
     82                     if (comparison(list[i],list[i+1])) {
     83                         T temp=list[i];
     84                         list[i]=list[i+1];
     85                         list[i+1]=temp;
     86                         swapped=true;
     87                     }
     88                 }
     89                 
     90             }while(swapped);
     91         }
     92     }
     93     
     94     class Person
     95     {
     96         public string Name{get;set;}
     97         public int Age{get;set;}
     98         
     99         public override string ToString()
    100         {
    101             return string.Format("Name:{0},Age:{1}",Name,Age);
    102         }
    103     }
    104 }
  4. 多播委托
     1 /*
     2  * 由SharpDevelop创建。
     3  * 用户: David Huang
     4  * 日期: 2015/7/30
     5  * 时间: 14:37
     6  */
     7 using System;
     8 
     9 namespace 多播
    10 {
    11     public static class MathOperations
    12     {
    13         public static void MutiplyByTwo(double value)
    14         {
    15             Console.WriteLine(string.Format("method: MutiplyByTwo, value: {0}, result: {1}",value,2*value));
    16         }
    17         
    18         public static void Square(double value)
    19         {
    20             Console.WriteLine(string.Format("method: Square, value: {0}, result: {1}",value,value*value));
    21         }
    22     }
    23     
    24     class Program
    25     {
    26         public static void Main(string[] args)
    27         {
    28             Action<double> mathOperations=MathOperations.MutiplyByTwo;
    29             mathOperations+=MathOperations.Square;
    30             
    31             DoTheMath(mathOperations,2.5);
    32             DoTheMath(mathOperations,3.5);
    33             
    34             Console.Write("Press any key to continue . . . ");
    35             Console.ReadKey(true);
    36         }
    37         
    38         static void DoTheMath(Action<double> action,double value)
    39         {
    40             action(value);
    41         }
    42     }
    43 }
  5. 匿名委托
     1 /*
     2  * 由SharpDevelop创建。
     3  * 用户: David Huang
     4  * 日期: 2015/7/30
     5  * 时间: 16:19
     6  */
     7 using System;
     8 
     9 namespace 匿名
    10 {
    11     class Program
    12     {
    13         public static void Main(string[] args)
    14         {
    15             Func<int,int> anonDel=delegate(int i)
    16             {
    17                 return i*2;
    18             };
    19             
    20             
    21             Console.WriteLine(anonDel(4));
    22             // TODO: Implement Functionality Here
    23             
    24             Console.Write("Press any key to continue . . . ");
    25             Console.ReadKey(true);
    26         }
    27     }
    28 }
posted @ 2015-07-30 16:23  David Huang  阅读(298)  评论(0编辑  收藏  举报