4

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

brainydexter
  • 19,826
  • 28
  • 77
  • 115

4 Answers4

7

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.

Community
  • 1
  • 1
retrodrone
  • 5,850
  • 9
  • 39
  • 65
0

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

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

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

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

Jonas
  • 4,454
  • 3
  • 37
  • 45
0

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.

Tim
  • 14,999
  • 1
  • 45
  • 68