委托,顾名思义,就是让其他代理,本质就是为具有共性方法组定义一个方法模板;(交流可以加qq群:568055323)

委托常见的方式有一般委托显示定义,Func<T,TResult> (T,表示传入参数的类型,TResult)表示返回的类型,可以最多有四个参数传入,

Action<T>,与Func的唯一区别是没有返回值;Predicate <T>就比较多单一了, 只能传入一个参数,然后返回一个bool值。

下面是示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace FuncTest
{
class Program
{
// 委托声明 -- 定义一个签名:
delegate double MathAction(double num);
static double Double(double input)
{
return input* ;
}
static void Main(string[] args)
{
//***********************普通委托
//使用一个方法实例化委托
MathAction ma = Double;
double result = ma(4.5);
Console.WriteLine(result.ToString()); //匿名方法
MathAction ma2 = delegate(double input)
{
return input * ;
};
Console.WriteLine(ma2()); //lambda表达式
MathAction ma3 = s => s * s * s;
Console.WriteLine(ma3()); //**********************Func<T,Tresult>委托
//利用Func,不用再显示定义委托
Func<double, double> ma4 = Double;
Console.WriteLine(ma4().ToString()); //利用Func,匿名方法
Func<double,double> ma5 = delegate (double input)
{
return input * input;
};
Console.WriteLine(ma5()); //利用Func,lambda表达式
Func<double, double> ma6 = s => s * s * s;
Console.WriteLine(ma6()); //和其他表达式结合
// 声明了一个Func委托类型的变量selector并用Lambda表达式进行实例化
// 这个Lambda表达式将用来获取一个字符串并将这个字符串转化为大写并返回
Func<string, string> selector = str => str.ToUpper(); // 创建一个字符串数组
string[] words = { "orange", "apple", "Article", "elephant" };
// 依次遍历这个字符串数组并调用委托实例selector进行处理
IEnumerable<String> aWords = words.Select(selector); // 输出结果到控制台
foreach (String word in aWords)
Console.WriteLine(word);
//**********************Action<T> 委托,与Func<T,TResult>唯一的区别就是没有返回值 Action<string> ma7 = delegate (string input)
{
Console.WriteLine(input);
};
ma7("from Action<string>"); //***********************Predicate<T>, 形式只有一个,传入一个参数,返回一个bool值
Predicate<string[]> predicate = delegate (string[] x)
{
var r = from p in x
where p.Contains("s")
select p;
if (r.ToList().Count > )
{
return true;
}
else
{
return false;
}
};
string[] _value = { "charlies", "nancy", "alex", "jimmy", "selina" }; if (predicate(_value))
{
Console.WriteLine("They contain.");
}
else
{
Console.WriteLine("They don't contain.");
} Console.ReadLine(); } }
}

最新文章

  1. pyqt信号和槽应用举例
  2. Linux一块网卡添加多个IP地址
  3. PostgreSQL的case when
  4. Linux平台屏幕录像工具RecordMyDesktop
  5. ECMAScript 6中的数组操作方法
  6. Google Chrome浏览器调试入门————转载只为自己查看方便
  7. hdu Inverting Cups
  8. iOS字符串加密至MD5&amp;及获取文件MD5
  9. 数据库插入某些数据会变成?,或则select无法读出数据库中的某些数据
  10. DedeCMS官方手册
  11. 不管肉鸡,还是代理,CC识别就封杀!
  12. c#中传递参数前加out
  13. mysql_config_editor程序的用法
  14. 创建理想的SEQUENCE和自增长的trigger
  15. 错误处理try catch
  16. 错误代码: 1054 Unknown column &amp;#39;t.createUsrId&amp;#39; in &amp;#39;group statement&amp;#39;
  17. leetcode — copy-list-with-random-pointer
  18. .net core 环境安装失败,错误:0x80072EE2
  19. Kafka 消费者相关配置
  20. HDU-1398 Square Coins(生成函数)

热门文章

  1. Java设计模式学习记录-GoF设计模式概述
  2. js中的模块化
  3. Dubbo 源码分析系列之一环境搭建
  4. IdentityServer4 中文文档 -3- (简介)已支持的规范
  5. 【SQL】小心在循环中声明变量——浅析SQL变量作用域
  6. 乐字节-Java8新特性之Base64和重复注解与类型注解
  7. (2)Jquery1.8.3快速入门_checkbox全选取消部分选中
  8. 性能监控(3)–linux下的iostat命令
  9. websocket学习和群聊实现
  10. Python3.4:splinter or traceback