如有不明白的地方欢迎加QQ群14670545 探讨
其实只需要记住一点,Action是无返回值的,Func是由返回值的,他们可以直接使用委托,也可以使用拉姆达表达式:
Func<string, bool> fn = TsetMothod;
Response.Write(fn("123"));
Response.Write("<br />");
//Func<string, string> ssFun = delegate(string s) { return s.ToUpper(); };
Func<string, string> selectors = str => str.ToUpper();
string[] arry = { "one", "two", "three", "four" };
//arry.Select(selectors).ToList().ForEach(x => Response.Write(x + " "));
IEnumerable<string> aWords2 = arry.Select(selectors);
foreach (string str in aWords2)
Response.Write(str + " "); //输出ONE TWO THREE FOUR
Response.Write("<br />");
Func<string, bool> testF = t => !string.IsNullOrEmpty(t); //直接表达式
Response.Write(testF("j"));
Response.Write("<br />");
int tempVal = 100;
List<Func<int>> intF = new List<Func<int>>();
intF.Add(() => tempVal + 1);
//value变量将在不同的操作中()=>value+1和()=>value+2 间共享数据
intF.Add(() => tempVal + 2);
//foreach (var f in intF)
//{
// int v = f();
// Response.Write(v.ToString() + ", ");
//}
intF.ForEach(x => Response.Write(x() + ", "));
Response.Write("<br />");
Func<int> funT = () => (15 % 2);
Response.Write(funT()); // 输出1
Response.Write("<br />");
Func<string, int> siFun = t => "abc".IndexOf(t);
CallMethod(siFun, "A");
Response.Write("<br />");
//Action<string> sAct = delegate(string s) { Response.Write(s); };
Action<string> sAct = x => Response.Write(x);
sAct("123");
public static bool TsetMothod(string name)
{
if (string.IsNullOrEmpty(name))
return false;
return true;
}