委托示例

最简单的委托示例:

 1 // 该委托可以指向任何传入两个整数并返回一个整数的方法
 2     public delegate int BinaryOp( int x, int y );
 3     public class SimpleMath
 4     {
 5         public int Add( int x, int y )
 6         { return x + y; }
 7         public int Subtract( int x, int y )
 8         { return x - y; }
 9         public static int SquareNumber( int a )
10         { return a * a; }
11     }
12     class Program
13     {
14         static void Main( string[] args )
15         {
16             Console.WriteLine("***** Simple Delegate Example *****\n");
17
18             SimpleMath m = new SimpleMath();
19
20             BinaryOp b = new BinaryOp(m.Add);
21             DisplayDelegateInfo(b);
22
23             Console.WriteLine("10 + 10 is {0}", b(10, 10));
24
25             Console.ReadLine();
26         }
27         static void DisplayDelegateInfo( Delegate delObj )
28         {
29             foreach (Delegate d in delObj.GetInvocationList())
30             {
31                 Console.WriteLine("Method Name: {0}", d.Method);
32                 Console.WriteLine("Type Name: {0}", d.Target);
33             }
34         }
35     }

支持多路广播的委托示例:

 1 /*
 2  * 2015.5
 3  * Car.cs
 4  */
 5 using System;
 6 using System.Collections.Generic;
 7 using System.Linq;
 8 using System.Text;
 9 using System.Threading.Tasks;
10
11 namespace CarDelegate
12 {
13     public class Car
14     {
15         // 内部状态数据
16         public int CurrentSpeed { get; set; }
17         public int MaxSpeed { get; set; }
18         public string PetName { get; set; }
19         private bool carIsDead = false;
20
21         // 构造函数
22         public Car() { MaxSpeed = 180; }
23         public Car( string name, int maxSp, int currSp )
24         {
25             CurrentSpeed = currSp;
26             MaxSpeed = maxSp;
27             PetName = name;
28         }
29
30         // 定义委托类型
31         public delegate void CarEngineHandler( string msgForCaller );
32
33         // 定义每个委托类型的成员变量
34         private CarEngineHandler listOfHandlers;
35
36         // 添加注册函数
37         public void RegisterWithCarEngine( CarEngineHandler methodToCall )
38         {   // 给每个委托对象添加多个方法
39             // 支持多路广播,可以使用 +=操作符,或直接调用Delegate.Combine()
40
41             // listOfHandlers += methodToCall;
42
43             if (listOfHandlers == null)
44                 listOfHandlers = methodToCall;
45             else
46                 Delegate.Combine(listOfHandlers, methodToCall);
47         }
48
49         public void UnRegisterWithCarEngine( CarEngineHandler methodToCall )
50         {   // 从委托的调用列表移除成员, 也可调用 Delegate.Remove()
51
52             listOfHandlers -= methodToCall;
53         }
54
55         public void Accelerate( int delta )
56         {
57             if (carIsDead)
58             {
59                 if (listOfHandlers != null)
60                     listOfHandlers("Sorry, this car is dead...");
61             }
62             else
63             {
64                 CurrentSpeed += delta;
65                 if (10 == (MaxSpeed - CurrentSpeed)
66                     && listOfHandlers != null)
67                 {
68                     listOfHandlers("Careful buddy!");
69                 }
70                 if (CurrentSpeed >= MaxSpeed)
71                     carIsDead = true;
72                 else
73                     Console.WriteLine("CurrentSpeed = {0}", CurrentSpeed);
74             }
75         }
76     }
77 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace CarDelegate
 8 {
 9     class Program
10     {
11         static void Main( string[] args )
12         {
13             Console.WriteLine("***** Delegates as event enablers *****\n");
14
15             Car c1 = new Car("BMW", 100, 10);
16
17             // 先绑定委托对象,稍后再注销
18             Car.CarEngineHandler handler2 = new Car.CarEngineHandler(OnCarEngineEvent2);
19             c1.RegisterWithCarEngine(handler2);
20
21             Console.WriteLine("***** Speeding up 1*****");
22             for (int i = 0; i < 6; i++)
23                 c1.Accelerate(20);
24
25             c1.UnRegisterWithCarEngine(handler2);
26
27             // 注销上一种方法,注册另一种方法
28             // 观察到两种方法的输出不一样
29             c1.RegisterWithCarEngine(new Car.CarEngineHandler(OnCarEngineEvent));
30             Console.WriteLine("***** Speeding up 2*****");
31             for (int i = 0; i < 6; i++)
32                 c1.Accelerate(20);
33
34             Console.ReadLine();
35         }
36
37         public static void OnCarEngineEvent( string msg )
38         {
39             Console.WriteLine("\n***** Message From Car Object *****");
40             Console.WriteLine("=> {0}", msg);
41             Console.WriteLine("***********************************\n");
42         }
43
44         public static void OnCarEngineEvent2( string msg )
45         {
46             Console.WriteLine("=> {0}", msg.ToUpper());
47         }
48     }
49 }

泛型Action<>和Func<>委托示例:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace ActionAndFuncDelegates
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Console.WriteLine("***** Fun with Action and Func *****\n");
14
15             // 使用泛型 Action<> 委托来指向DisplayMessage, Action<>可以指向多至16个参数
16             Action<string, ConsoleColor, int> actionTarget =
17                 new Action<string, ConsoleColor, int>(DisplayMessage);
18
19             actionTarget("Action Message!", ConsoleColor.Green, 5);
20
21             // 使用泛型 Func<> 委托
22             Func<int, int, int> funcTarget = new Func<int, int, int>(Add);
23             int result = funcTarget.Invoke(40, 40);
24             Console.WriteLine("40 + 40 = {0}", result);
25
26             Func<int, int, string> funcTarget2 = new Func<int, int, string>(SumToString);
27             string sum = funcTarget2(90, 300);
28             Console.WriteLine(sum);
29
30             Console.ReadLine();
31         }
32
33         static void DisplayMessage(string msg, ConsoleColor txtColor, int printCount)
34         {
35             // 设置命令行文本颜色
36             ConsoleColor previous = Console.ForegroundColor;
37             Console.ForegroundColor = txtColor;
38
39             for (int i = 0; i < printCount; i++)
40             {
41                 Console.WriteLine(msg);
42             }
43             // 重置颜色
44             Console.ForegroundColor = previous;
45         }
46
47         static int Add(int x, int y)
48         {
49             return x + y;
50         }
51
52         static string SumToString(int x, int y)
53         {
54             return (x + y).ToString();
55         }
56     }
57 }

在事件注册时直接将一个委托与一段代码相关联称为匿名方法。使用 +=语法处理事件时被内联定义。

如:  

Car c1 = new Car("BMW", 100, 10);

c1.AboutToBlow += delegate(object sender, CarEventArgs e)
{
     aboutToBlowCounter++;
         Console.WriteLine("Message from Car: {0}", e.msg);

};

C#的Lambda 表达式都使用 Lambda 运算符 =>,该运算符读为“goes to”。语法如下:

形参列表=>函数体

函数体多于一条语句的可用大括号括起。

Lambda 表达式可以用于任何匿名方法或者强类型委托可以应用的场合,而且比匿名方法更节省编码时间。其实编译器只是把表达式编译为使用委托Predicate<T>的标准匿名方法而已。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6
 7 namespace LambdaExpressionsMultipleParams
 8 {
 9     public class SimpleMath
10     {
11         // 多个参数
12         public delegate void MathMessage( string msg, int result );
13         private MathMessage mmDelegate;
14
15         public void SetMathHandler( MathMessage target )
16         { mmDelegate = target; }
17
18         public void Add( int x, int y )
19         {
20             if (mmDelegate != null)
21                 mmDelegate.Invoke("Adding has completed!", x + y);
22         }
23     }
24
25     class Program
26     {
27         static void Main( string[] args )
28         {
29             // 使用Lambda表达式注册委托
30             SimpleMath m = new SimpleMath();
31             m.SetMathHandler(( msg, result ) =>
32             { Console.WriteLine("Message: {0}, Result: {1}", msg, result); });
33
34             // 执行Lambda表达式
35             m.Add(10, 10);
36             Console.ReadLine();
37         }
38     }
39 }
时间: 2024-08-05 09:40:00

委托示例的相关文章

事件和委托示例,每一行都有注释

using System; delegate void CharEventHandler(object source, CharEventArgs e); //先声明一个委托,object为事件源,XXXXEventArgs由EventArgs(该类用于将数据传给事件)派生而来; public class CharEventArgs : EventArgs { //EventArgs派生类格式,就一个构造函数接受一个字符赋给数据成员; public char currchar; //数据成员;

jQuery之事件和批量操作、事件委托示例

一.常用事件 click(function(){...}) // 点击时触发 focus(function(){...}) // 获得焦点触发 blur(function(){...}) // 失去焦点触发 change(function(){...}) // 内容改变后触发 keyup(function(){...}) // 键盘按下后触发 keydown(function(){...}) // 键盘放开后触发 hover(function(){...},function(){...}) //

Action匿名委托示例

class Program { static void Main(string[] args) { string mid = ",mid"; Action<string> anonDel = delegate (string param) { param += mid; param += " and this."; Console.WriteLine(param); }; anonDel += delegate (string t) { Console.

Func委托示例

class BubbleSorter { public static void Sort<T>(IList<T> sortArray, Func<T, T, bool> comparison) { bool swapped = true; do { swapped = false; for (int i = 0; i < sortArray.Count - 1; i++) { if (comparison(sortArray[i + 1], sortArray[i

C#委托讲解以及示例演示

什么是委托 通俗解释:委托就是一个能存放符合某种格式(方法签名)的方法的指针 自己理解:委托就是定义一个变量来存放方法,这个委托可以存放多个方法,在调用的时候,会按照添加的次序来执行添加的方法 对委托的理解 其实委托类似于C语言中的指针,他是一种数据类型的名字,例如int.double等,只不过,指针类型存放的是方法. 委托本身就是一个类,可以写在类的里面或者类的外边,本身是一个语法糖,在编译的时候会编译成一个类. 委托的作用 因为委托是一个存放方法的数据类型,所以委托的作用如下 将方法作为参数

委托+内置委托方法

委托概念:如果我们要把方法当做参数来传递的话,就要用到委托.简单来说委托是一个类型,这个类型可以赋值一个方法的引用. 声明委托: 在C#中使用一个类分两个阶段,首选定义这个类,告诉编译器这个类由什么字段和方法组成的,然后使用这个类实例化对象.在我们使用委托的时候,也需要经过这两个阶段,首先定义委托,告诉编译器我们这个委托可以指向哪些类型的方法,然后,创建该委托的实例. 定义委托的语法如下: delegate void IntMethodInvoker(int x); 定义了一个委托叫做IntMe

C#委托和事件定义和使用

委托 定义委托的语法和定义方法比较相似,只是比方法多了一个关键字delegate ,我们都知道方法就是将类型参数化,所谓的类型参数化就是说该方法接受一个参数,而该参数是某种类型的参数,比如int.string等等:而委托是将方 法参数化,说了上面的那个类型参数化之后,相信你也能猜到方法参数化的意思了,对,就是将方法作为一个参数传到一个委托中. 首先来看看声明委托的语句: public deletate void MyDelegate(); public:访问修饰符  delegate:关键字 

委托和事件

一.委托:是一个能够引用方法的对象.创建委托时实际是创建一个方法引用的对象,创建的引用对象能够调用方法. 委托调用可以调用不同的方法,只需改变方法的引用即可.即委托调用方法是运行时确定,而非编译时确定. 就像声名一个object实例一样,声名的只是一个占位符,具体指向哪个对象在运行时可以动态指定.在委托中指定方法有限制:返回值,参数类型要相同. 委托声名:delegate ret-type delegatename(parameter-list) delegate 关键字声名委托 ret-typ

初学委托

首先要知道委托是什么,委托实际上就相当于C语言中的函数指针 一 声明委托 示例:delegate void StringProcessor(string input); 这是一个返回值为空的委托声明,其中声明的参数必须是string 类型,同时比较写出返回类型(这个类型必须与委托指向的方法的返回值的类型一致); 二 创建委托示例 示例 : jonProcessor = new StringProcessor(jon.Say); tomProcessor = new StringProcessor