13 C# 第十二章 委托
来源:互联网 发布:易经入门 知乎 编辑:程序博客网 时间:2024/05/21 11:16
委托概述:
与C/C++中的函数指针类似,把方法用对象封装起来,以便在运行时间接的绑定该方法。委托的使用:
关键字: delegatepublic delegate bool ComparsonHandler(int x, int y);
using System;using System.Collections.Generic;using System.Text;namespace Delegate{ class DelegateSample { public delegate bool PrintHeaderHandler(int x, int y); public static bool PrintHeaderA(int x, int y) { Console.WriteLine("A func ===> X===={0}, Y===={1}", x, y); return true; } public static bool PrintHeaderB(int x, int y) { Console.WriteLine("B func ===> X::::{0}, Y::::{1}", x, y); return true; } public static void Tester(int a, int b, PrintHeaderHandler printer) { printer(a, b); } } class Program { static void Main(string[] args) { DelegateSample.Tester(1, 2, DelegateSample.PrintHeaderA); DelegateSample.Tester(5, 10, DelegateSample.PrintHeaderB); Console.ReadKey(); } }}
匿名方法
没有实际方法声明的委托的实例,或者说,他们的定义是直接内嵌在代码中的。使用匿名方法 -- 声明变量
using System;using System.Collections.Generic;using System.Text;namespace Delegate{ class DelegateSample { public delegate bool PrintHeaderHandler(int x, int y); public static void Tester(int a, int b, PrintHeaderHandler printer) { printer(a, b); } static void Main(string[] args) { PrintHeaderHandler printerMethod; // 这里没有一个实际的方法,而是直接实现了委托所需要的函数的功能。 printerMethod = delegate(int x, int y) { Console.WriteLine("声明变量,在变量中实现 ===> X::::{0}, Y::::{1}", x, y); return true; }; DelegateSample.Tester(5, 10, printerMethod); Console.ReadKey(); } }}
使用匿名方法 -- 不声明变量
using System;using System.Collections.Generic;using System.Text;namespace Delegate{ class DelegateSample { public delegate bool PrintHeaderHandler(int x, int y); public static void Tester(int a, int b, PrintHeaderHandler printer) { printer(a, b); } static void Main(string[] args) { // 这里直接实现了委托所需要的函数的功能。 DelegateSample.Tester(5, 10, delegate(int x, int y) { Console.WriteLine("不声明变量 ===> X::::{0}, Y::::{1}", x, y); return true; }); Console.ReadKey(); } }}
匿名函数之 Lambda表达式
一种匿名函数语法,比匿名方法更加简洁。匿名函数和Lambda表达式之间的关系。
语句 Lambda,为匿名方法提供的一种简化语法,它不包含delegate 关键字,但添加了Lambda 运算符 =>
实例程序:
using System;using System.Text;namespace Delegate{ class DelegateSample { public delegate bool PrintHeaderHandler(int x, int y); public static void Tester(int a, int b, PrintHeaderHandler printer) { printer(a, b); } static void Main(string[] args) { // 这里直接实现了委托所需要的函数的功能。 // 这里指定了x, y 的参数类型 DelegateSample.Tester(5, 10, (int x, int y)=> { Console.WriteLine("Lambda 表达式 ===> X::::{0}, Y::::{1}", x, y); return true; }); // 参数x, y 的类型也可以由编译器来推断 DelegateSample.Tester(15, 20, (x, y)=> { Console.WriteLine("Lambda 表达式 ===> X::::{0}, Y::::{1}", x, y); return true; }); // 无参数的Lambda语句, 这里需要一个空括号 DelegateSample.Tester(15, 20, () => { Console.WriteLine("语句 Lambda 无参数, 此处使用空括号 ===> X::::无参数, Y::::无参数"); return true; }); // 只有一个参数时 Lambda可以不使用括号,这是一个例外 DelegateSample.Tester(25, 30, OneParam => { OneParam = 25 + 30; Console.WriteLine("语句 Lambda 只有一个参数, 此处省略括号 ===> OneParam::::{0}", OneParam); return true; }); Console.ReadKey(); } }}
表达式 Lambda: 只有一个表达式,而不是语句块(包含多个语句)。
using System;using System.Text;namespace Delegate{ class DelegateSample { public delegate bool CompareHandler(int x, int y); public static void Tester(int a, int b, int c, CompareHandler comparer) { // if a > b regure true Console.WriteLine("Compare result = {0}", comparer(a, b)); } static void Main(string[] args) { // Lambda 表达式 DelegateSample.Tester(100, 99, 1, (x, y) => x < y); Console.ReadKey(); } }}
Lambda 表达式的实质: 把一组指令赋值给一个变量,它可以传递一组能在不同位置调用的指令。
Lambda 表达式(和匿名方法)并不是CLR内部固有的构造,它们的实现是由C#编译器在编译时生成的。
Lambda 主要是替代了C#2.0中的匿名方法。
所以Lambda表达式本身是没有类型的。所以Lambda表达式不存在任何的成员。
由此下面的一些操作是错误的:
1) 操作符"."是不起作用的。
例: Type type = ((int x) =>).ToString(); 是错误的。
2) Lambda 表达式 是不能够 出现在 is 或 as 的左边的。
例: bool boolean = ((int x) =>) is Func<int , int>;
3) 不能够将Lambda 表达式赋值给隐式类型,因为Lambda本身无类型,这时编译器不知道要生成什么类型的变量。
例: var thing = (x => x)
- 13 C# 第十二章 委托
- 第十二章 委托和Lambda表达式
- C#入门经典第十二章
- ACCP S1 C#第十二章
- C#委托-委托概述
- C#委托
- C#委托
- C#委托
- c#委托
- C# 委托
- C#委托
- C# 委托
- c#委托
- C#委托
- c#委托
- C#委托
- C# - 委托
- C#委托
- jni的使用
- 利用SqlDataReader读取大量的数据库数据
- IOS 深入浅出Cocoa之 sqlite 数据库
- 一个死锁的简单例子
- 第三次上机实验
- 13 C# 第十二章 委托
- 字符、字符数组、char、string的区别分析
- Extjs-布局
- linux加硬盘
- 开博第一篇文章
- 指针与数组的区别
- 类成员函数的重载、覆盖和隐藏区别
- tomcat启动时卡在couchbase连接上的解决办法(真凶原来是avast)
- 简述数组与指针的区别