Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Call Non-Trailing Arguments as Default Argument in C#?

  • Last Updated : 22 Dec, 2021

C# provides a great feature to function that is a function argument can have default values. When a function is called without the argument, the argument gets its default value. Or in other words, a default argument is a value provided in a method declaration that is automatically assigned if the value for the argument is not provided during the function call then the argument will be assigned with a default value. The default value is assigned by using the assignment(=) operator like this “keywordname = value”.

  • The default value of the default argument is a constant expression.
  • Every default argument holds a default value which is part of its definition.
  • The default argument is always defined at the end of the parameter list in the methods. When we use a default argument other than the last parameter then the compiler will give an error.
  • It is also known as option parameter.

Syntax:

public void methodName(arg1, arg2, arg3 = default_Value, arg4 = default_Value)

Call a non-trailing argument and default argument

When we call a non-trailing argument as default argument normally it will not work as expected. It can be better understood by the following example.

static void defArgs(int a, int b = 1, int c = 2)   

{

    // Printing the values

    Console.WriteLine(“value of a = ” + a + “, ” + “value of b = ” + 

                                                               b + “, ” + “value of c = ” + c);

}

// We want to assign b value as its default value and pass a, c as arguments to the defArg method

defArgs(100, 200);

Here, we wanted to assign 100 to a and 200 to c and leave b with its default value, But what actually happens is the 100 is assigned to a, 200 is assigned to b leaving c with its default value. We can call b (i.e. Non-trailing argument) as the default argument by using parameter_name and colon for the arguments following the b, when calling the defArg() method

Approach:

  • Create a method, for example defArgs(). In the method definition, take 3 arguments and for the 2nd and 3rd argument assign default values using assignment operator(=).
static void defArgs(int a, int b = 1, int c = 2, int d = 3){}
  • Now, from the main method call the non-trailing arguments of defArg() method as default argument. For example defArg(100, 200), here we want to assign 100 to a and 200 to c, and wanted to assign default value to b. So, using parameter_name and colon for the arguments following the b when calling defArg() method.
defArgs(100, c:200); 

Here 100 assigned to a, 200 assigned to c and b is left with its default value.

  • In the defArg() method print the values of the arguments using Console.WriteLine() method.

C#




// C# program to call non-trailing arguments
// as default argument in C#
using System;
 
class GFG{
 
// Driver code   
public static void Main()
{
     
    // Calling b, d as default arguments.
    defArgs(100, c: 200);
 
    // Calling b as default argument.
    defArgs(100, c: 200, d: 300);
}
 
// Method with default arguments
// taking default value of b as 1,
// and default value of c as 2 and d as 3.
static void defArgs(int a, int b = 1, int c = 2, int d = 3)
{
 
    // Printing the values
    Console.WriteLine("value of a = " + a + ",\t" +
                      "value of b = " + b + ",\t" +
                      "value of c = " + c + ",\t" +
                      "value of d = " + d);
}
}
Output
value of a = 100,    value of b = 1,    value of c = 200,    value of d = 3
value of a = 100,    value of b = 1,    value of c = 200,    value of d = 300

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!