Action <T>内部的三元运算符无法正常工作

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)

给出旧的"仅赋值,减量等允许"错误.

这有可能吗?

Red*_*dog 5

你必须这样做:

var action = new Action<string>(str => Console.WriteLine((str == null) ? "isnull" : "isnotnull"));
Run Code Online (Sandbox Code Playgroud)