Zhonghui

每个不曾起舞的日子,都是对生命的辜负

User Tools

Site Tools


程序:csharp:lambda表达式

Lambda表达式


=>用于lambda表达式

using System;
 
Action<string> greet = name => 
{ 
 string greeting = $”Hello {name}!;
 Console.WriteLine(greeting);
};
 
// (input) => () 后面用小括号可以吗
 
Action obj1;
obj1 = () => { Debug.Log("FGNB"); };
 
Action<int> obj2;
obj2 = (int x) => { Debug.Log(x); };
obj2 = (x) => { Debug.Log(x+233); };//也可以省略参数类型,最好不要
 
Func<int,int> obj3 = (int x) => { return x; };
 
Func<int, bool> equalsFive = x => x == 5; // 如果就是一个变量,不需要后面加括号,也不需要return
bool result = equalsFive(4);
Console.WriteLine(result); 
 
// Action没有返回值
// Func有返回值
/var/www/DokuWikiStick/dokuwiki/data/pages/程序/csharp/lambda表达式.txt · Last modified: 2022/11/18 03:20 by zh