While going through the ASP.NET MVC docs I see this idiom being used alot:
new { foo = "bar", baz = "foo" }
Is this a Dictionary literal syntax?
Is it a new class/struct with the type inferred by the called function definition?
If it is how come the vars don't need a type definition, not even var
?
When used as a declaration modifier, the new keyword explicitly hides a member that is inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base class version.
Using new stdClass() to create an object without class: For creating an object without a class, we will use a new stdClass() operator and then add some properties to them.
When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.
In method hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword. Or in other words, in method hiding, you can redefine the method of the base class in the derived class by using the new keyword.
This is an anonymous type.
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.
http://msdn.microsoft.com/en-us/library/bb397696.aspx
Anonymous types are strongly typed. From the perspective of the common language runtime, an anonymous type is no different from any other reference type.
If two or more anonymous types in the same assembly have the same number and type of properties, in the same order, the compiler treats them as the same type. They share the same compiler-generated type information.
Anonymous types should not be passed between assemblies or even as return values from methods (possible, but rarely, rarely advisable).
Anonymous types are a convenience mechanism, e.g. when working with LINQ, such as the following projection:
LINQ Example
var result = myEnumerable.Select( o => new { foo = o.Foo, bar = o.Bar } );
// "result" is an enumerable of a new anonymous type containing two properties
Other Questions
Is this a Dictionary literal syntax?
No, though there are many similarities. ASP .Net MVC uses RouteValueDictionary
and anonymous types to represent the same information in many method overloads.
how come the vars don't need a type definition, not even var?
Value types are inferred, though inference is not always possible: http://msdn.microsoft.com/en-us/library/bb531357.aspx (VB version, if someone knows the URL of the c# equivalent please update)
This is an anonymous type syntax. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With