1
为什么以及是如何工作的下面的代码动态与三元运营商
dynamic x = (c== 'a') ? new D1() :x= new D2();
但不是这个
dynamic x = (c== 'a') ? new D1() : new D2();
代码
class Program
{
static void Main(string[] args)
{
var c = Console.ReadKey().KeyChar;
dynamic x = (c== 'a') ? new D1() :x= new D2();
x.Print();
Console.ReadKey();
}
}
class D1
{
public void Print()
{
Console.WriteLine("D1");
}
}
class D2
{
public void Print()
{
Console.WriteLine("D2");
}
}
你得到了什么错误?至少我会尝试添加一些括号,以使代码更具可读性。 – PMF