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