Javatpoint Logo
Javatpoint Logo

C# Named and Optional Arguments


C# Named Arguments

This 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 Example

Output

Rahul Kumar
Rahul Kumar
Kumar Rahul

C# Optional Arguments

In 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 1

Output

24
20

We can make any number of optional parameter s. let's see an example.

C# Optional Arguments Example 2

Output

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 3

Output

error CS1737: Optional parameters must appear after all required parameters






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA