Yinon Dotan
Yinon Dotan

Reputation: 147

routes.MapRoute syntax

This is a syntax of a routes.MapRoute function in mvc.

  routes.MapRoute(
     { controller = "Home", action = "HomePage" }
  );  

The first argument of but instead of a simple string inside double quotation marks "" it has a prefix { name : } not in quotation marks.
I have never seen that before .Can someone explain how this works.

Upvotes: 0

Views: 364

Answers (2)

D-Shih
D-Shih

Reputation: 46219

It is Named Arguments, which support upper C# 4

Named arguments free you from the need to remember or to look up the order of parameters in the parameter lists of called methods. The parameter for each argument can be specified by parameter name. For example, a function that prints order details (such as, seller name, order number & product name) can be called in the standard way by sending arguments by position, in the order defined by the function.

If you do not remember the order of the parameters but know their names, you can send the arguments in any order.

Upvotes: 1

John H
John H

Reputation: 14655

What you're describing are called Named Arguments, and were introduced with C# 4.0.

Named arguments free you from the need to remember or to look up the order of parameters in the parameter lists of called methods. The parameter for each argument can be specified by parameter name. For example, a function that prints order details (such as, seller name, order number & product name) can be called in the standard way by sending arguments by position, in the order defined by the function.

PrintOrderDetails("Gift Shop", 31, "Red Mug");

If you do not remember the order of the parameters but know their names, you can send the arguments in any order.

PrintOrderDetails(orderNum: 31, productName: "Red Mug", sellerName: "Gift Shop");

PrintOrderDetails(productName: "Red Mug", sellerName: "Gift Shop", orderNum: 31);

Upvotes: 1

Related Questions