using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DelegateDemo1
{
class Program
{
static void One()
{
Console.WriteLine("one");
throw new Exception("error in one");
}
static void Two()
{
Console.WriteLine("Two");
}
static void Main(string[] args)
{
string mid = ",middle part,";
/*Func<string, string>:接受一个字符串参数,返回一个字符串 在这个匿名方法中不能使用
break,goto,continue跳到函数外部,也不能够跳到内部
*/
Func<string, string> anonDel = delegate(string param)
{
param += mid;
param += "and this is end";
return param;
};
Console.WriteLine(anonDel("start"));
/*lambda =>:左边列出了所需要的参数(param)
lambda => :右边定义了赋予lambda变量的方法实现的代码
*/
Func<string, string> lambda = param =>
{
param += mid;
param += "and this is end";
return param;
};
Console.WriteLine(anonDel("lambda"));
//lambda一个参数 编译器会添加一条隐式return语句
Func<string, string> oneParam = s => string.Format("change uppercase {0}", s.ToUpper());
//添加显示的return语句
Func<string, string> oneParamR = s =>
{
return string.Format("change uppercase {0}", s.ToUpper());
};
Console.WriteLine("oneParam is " + oneParam("Test"));
//lambda两个参数 编译器会添加一条隐式return语句
Func<double, double, double> towParams = (x, y) => x * y;
//添加显示的return语句
Func<double, double, double> towParamsR = (x, y) =>
{
return x * y;
};
Console.WriteLine("towParams is " + towParams(3,2));
Console.ReadKey();
}
}
}