Question
What are Named and Optional arguments in C#?
Answer
While passing the arguments you can associate a specific parmeter by its name rather than passing the argument by its position, this is what the Named argument does.
Also you can omit certain parameters which are called as Optional parameter.
The point here is even if you don't remember the parameter order and you know the parameter name than this way of passing the parameter without thinking about the order will help a lot. Also it improves the readability of the code.
For example( from MSDN)
CalculateBMI(weight: 123, height: 64);
CalculateBMI(height: 64, weight: 123);
N.B: A named argument can follow positional arguments. However, a positional argument cannot follow a named argument, this will cause a compiler error.
Read More..
 
[110 clicks]
Published under:
C# Interview Questions and Answers · · · ·