How can i extract value of properties from anonymous class?
When i declare
object o = new { name = "Bruce",Age=21 };
Console.WriteLine("name={0},age={1}"开发者_运维知识库,???,??? );
Now how can i print value of name and age?
Dont assign to an object variable, use var:
var o = new { name = "Bruce", Age = 21 };
Console.WriteLine( "name={0},age={1}, o.name, o.Age );
While not accessing the properties directly (see LBushkin's answer). ToString() is overloaded to list the content of all of the properties
var o = new { name = "Bruce", Age = 21 };
Console.WriteLine(o);// { name = Bruce, Age = 21 }
精彩评论