What does the following syntax mean in C #

I learn something new in everyday life about C # and stumbled upon this design. I am not 100% sure what he is doing, so someone can explain this:

new { Name = "John"} 

This was used where the string was expected as an argument to a method call.

thanks

+4
source share
4 answers

This is the object initializer for the anonymous class. It creates an object with a single Name property, with the value "John". Since you have no way to refer to an object, you should use it immediately, as in the LINQ statement, or as a parameter, as you mentioned.

See also this answer .

+7
source

Its new anonymous type with the Name property set to the string "John" .

See: http://msdn.microsoft.com/en-us/library/bb397696.aspx

0
source

This is a new syntax known as anonymous types. You can read here for more details.

0
source

Well, it seems to me that it creates an anonymous type with one property (Name, type strings).

But saying that he used where the string was expecting, I was a little embarrassed.

0
source

All Articles