委托
2016-01-06 20:11
441 查看
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; namespace ConsoleApp { class Program { delegate bool DelCompare(int a,int b); static void Main(string[] args) { DelegateFunction(); AnonymousDelegateFunction(); LambdaExpressionFunction(); FuncFunction(); ExpressionFunction(); Console.ReadKey(); } /// <summary> /// 委托 /// </summary> private static void DelegateFunction() { DelCompare d = Compare; var val= d(3, 5); Console.WriteLine(String.Format("DelegateFunction:{0}", val)); } /// <summary> /// 匿名委托 /// </summary> private static void AnonymousDelegateFunction() { DelCompare d = delegate (int a, int b) { return a > b; }; var val = d(3, 5); Console.WriteLine(String.Format("AnonymousDelegateFunction:{0}", val)); } /// <summary> /// Lambda表达式 /// </summary> private static void LambdaExpressionFunction() { DelCompare d = (a, b) => a > b; var val = d(3, 5); Console.WriteLine(String.Format("LambdaExpressionFunction:{0}", val)); } /// <summary> /// Func /// </summary> private static void FuncFunction() { Func<int, int, bool> d = (a, b) => a > b; var val = d(3, 5); Console.WriteLine(String.Format("FuncFunction:{0}", val)); } private static void ExpressionFunction() { Expression<Func<int,int, bool>> exp = (a,b) => a > b; //生在表达式 Func<int,int, bool> fun = exp.Compile(); //编辑表达式 var val= fun(3,5); //执行表达式 Console.WriteLine(String.Format("ExpressionFunction:{0}", val)); } private static bool Compare(int a,int b) { return a > b; } } }
结果:
原文链接:
/article/7180687.html
点击打开链接
相关文章推荐
- [编程题] 汽水瓶
- 每天一个Linux之du命令
- 学习了下简单的光照模型
- 在Windows Server 2008 R2上安装Ftp服务
- Debian 项目组向 Ian Murdock 的逝世表示哀悼
- 3.Longest Substring Without Repeating Characters(7.66%)
- 【POJ】[1852]Ants
- 机器学习入门系列三(关键词:逻辑回归,正则化)
- jQuery Dialog API (一)
- 淘宝的架构 -君子不器原创文章
- java jar war 打包
- jquery uploadify插件多文件上传
- Spark NMF(非负矩阵分解)算法实现与测试
- binder驱动-接收缓存区管理
- java+selenium Q&A
- Java中的抽象类与接口
- 安卓相对布局的基本属性
- 隐马尔可夫模型(HMM)
- 如何制作gif图片?如何制作你项目的动态效果图到你的csdn?
- 【南理oj】6 - 喷水装置(一)(贪心)