tags:

views:

841

answers:

3

Can someone explain how New works with the With keyword in this example from the MVC framework.

routes.MapRoute("Default", 
                "{controller}/{action}/{id}", 
                New With {.controller = "Home", .action = "Index", .id = ""})
A: 

It creates a new anonymous class with the specified properties.

More information from MSDN.

Adrian Godong
A: 

Here "new" is declaring an anonymous class.

This class has no formal definition (i.e. no "Public Class " definition), it's structure is defined by the items in the curly's after the with. So it defines and constructs a class in a single statement.

Binary Worrier
+1  A: 

This syntax is used to create an anonymous type in VB.Net.

It allows you to define a type on the fly with a set of name / value pairs. The names all turn into properties on the type. If you open up the generated assembly in reflector you will be able to see these types.

Don't let the With syntax portion fool you. This feature has nothing to do with the "With" context feature of VB.Net. Other than the "." prefix on the value names.

JaredPar