Sea*_*man 3 c# lambda delegates ternary-operator
有一个Action委托并尝试在lambdas中使用其中的三元运算符:
Action<string> action = new Action<string>( str => (str == null) ?
Console.WriteLine("isnull") : Console.WriteLine("isnotnull")
Run Code Online (Sandbox Code Playgroud)
给出旧的"仅赋值,减量等允许"错误.
这有可能吗?
你必须这样做:
var action = new Action<string>(str => Console.WriteLine((str == null) ? "isnull" : "isnotnull"));
Run Code Online (Sandbox Code Playgroud)