RQD*_*QDQ 180
当然:
List<String> items = new List<string>();
var results = items.Where(i =>
{
bool result;
if (i == "THIS")
result = true;
else if (i == "THAT")
result = true;
else
result = false;
return result;
}
);
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 26
(我假设你真的在谈论多个语句而不是多行.)
您可以使用大括号在lambda表达式中使用多个语句,但只有不使用大括号的语法才能转换为表达式树:
// Valid
Func<int, int> a = x => x + 1;
Func<int, int> b = x => { return x + 1; };
Expression<Func<int, int>> c = x => x + 1;
// Invalid
Expression<Func<int, int>> d = x => { return x + 1; };
Run Code Online (Sandbox Code Playgroud)
Func<string, bool> test = (name) =>
{
if (name == "yes") return true;
else return false;
}
Run Code Online (Sandbox Code Playgroud)
从C#7开始:
单行语句:
int expr(int x, int y) => x + y + 1;
Run Code Online (Sandbox Code Playgroud)
多行语句:
int expr(int x, int y) { int z = 8; return x + y + z + 1; };
Run Code Online (Sandbox Code Playgroud)
尽管这些被称为局部函数,但我认为这看起来比以下的更为干净,并且实际上是相同的
Func<int, int, int> a = (x, y) => x + y + 1;
Func<int, int, int> b = (x, y) => { int z = 8; return x + y + z + 1; };
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
55402 次 |
最近记录: |