brainydexter
brainydexter

Reputation: 20386

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

Upvotes: 4

Views: 186

Answers (4)

retrodrone
retrodrone

Reputation: 5920

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.

Upvotes: 7

Tim
Tim

Reputation: 15237

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.

Upvotes: 0

Jonas
Jonas

Reputation: 4584

This is a newer syntax known as Anonymous Types. You can read here for more detailed information.

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 191037

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

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

Upvotes: 0

Related Questions