您的位置:首页 > 其它

委托

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

点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: