简体   繁体   中英

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 does, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM