Optional and Named Arguments in C#

One of the new interesting features in Visual Studio 2010 is 'Named and Optional' arguments. Practically, they are very useful together but both are quite different.
Optional Arguments
Optional Arguments enable us to omit arguments for some parameters. Any call must provide arguments for all required parameters but can omit arguments for optional parameters. The parameter which is omitted has default value as part of its definition. If no argument is sent for that the default value automatically will be used. Optional parameters are defined at the end of the parameter list after any required parameters.
In above example, we are not sending the value for 'width' parameter because it is Optional. If you try to send the 'width' parameter value then it will overwrite existed value. For example:
In above example, width value will be replaced by 6.
Named Arguments
Named Arguments enable us to specify an argument for a particular parameter by associating the argument with the parameter's name rather than with the parameter's position in the parameter list. It frees us 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.
In above example, if we replace the non-named arguments by named-arguments then, will have not tension about the order of arguments.


Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System

Lambda two tables and three tables inner join code samples