what does the following syntax mean in C#
I learn something new everyday about C# and came across this construct. I am not 100% sure what it doe开发者_如何学Gos, so can someone please explain it:
new { Name = "John"}
This was used where a string was expected as an argument to a method call.
Thanks
It's an object initializer for an anonymous class. It constructs an object with a single property, Name, with value "John." Since you have no way to refer to the object, you would use it right away, as in a LINQ statement or as a parameter as you mentioned.
See also this answer.
Its a new anonymous type with the property Name
set to the string "John"
.
See: http://msdn.microsoft.com/en-us/library/bb397696.aspx
This is a newer syntax known as Anonymous Types. You can read here for more detailed information.
Well, it looks to me like its creating an anonymous type with one property (Name, of type string).
But saying that it's used where a string was expected has me a little confused.
精彩评论