C# Named and Optional ArgumentsC# Named ArgumentsThis feature allows us to associate argument name with its value at the time of function calling. When we make named arguments, the arguments are evaluated in the order in which they are passed. It helps us, not to remember the order of parameters. If we know the parameters names, we can pass that in any order. Let's see an example. C# Named Arguments ExampleOutput Rahul Kumar Rahul Kumar Kumar Rahul C# Optional ArgumentsIn C#, a method may contain required or optional parameters. A method that contains optional parameters does not force to pass arguments at calling time. It means we call method without passing the arguments. The optional parameter contains a default value in function definition. If we do not pass optional argument value at calling time, the default value is used. Note: We must define optional parameters at the end of parameter list.C# Optional Arguments Example 1Output 24 20 We can make any number of optional parameter s. let's see an example. C# Optional Arguments Example 2Output 24 24 24 Make sure, all the optional parameters are placed at the end of the parameter list. Otherwise, compiler throws a compile-time error. Let's see an example. C# Optional Arguments Example 3Output error CS1737: Optional parameters must appear after all required parameters Next TopicC# Asynchronous Methods |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India