当我宣布
时object o = new { name = "Bruce",Age=21 };
Console.WriteLine("name={0},age={1}",???,??? );
现在我如何打印姓名和年龄的价值?
答案 0 :(得分:6)
不要分配给对象变量,请使用var:
var o = new { name = "Bruce", Age = 21 };
Console.WriteLine( "name={0},age={1}, o.name, o.Age );
答案 1 :(得分:3)
虽然没有直接访问属性(请参阅LBushkin的回答)。重载ToString()以列出所有属性的内容
var o = new { name = "Bruce", Age = 21 };
Console.WriteLine(o);// { name = Bruce, Age = 21 }