Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new keyword without class name in c#

Tags:

c#

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?

like image 797
benj Avatar asked Oct 05 '12 03:10

benj


People also ask

What is the use of new keyword in C?

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.

Can we create an object without class?

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.

Can you use new with a struct?

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.

Which keyword is used to hide a base class member in C#?

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.


2 Answers

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)

like image 110
Tim M. Avatar answered Sep 22 '22 21:09

Tim M.


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.

like image 21
Kale McNaney Avatar answered Sep 22 '22 21:09

Kale McNaney