As the name suggests optional parameters are not compulsory parameters, they are optional. It helps to exclude arguments for some parameters. Or we can say in optional parameters, it is not necessary to pass all the parameters in the method. This concept is introduced in C# 4.0. Here we discuss the different ways to implement the optional parameter. In C#, there are 4 different types of implementation of optional parameters are available as follows:
- By using default value: You can implement optional parameters by using default value. It is the simplest and easiest way to implement the optional parameter. In this way, you just simply define the optional parameters with their default value in the method definition. And always remember the optional parameter is the last parameter in the parameter list of the method. In default value method, when you do not pass the value of the optional parameters, then the optional parameters use their default value and when you pass the parameters for optional parameters, then they will take the passed value not their default value.
Example: Here, we have two regular parameters, i.e. str1 and str2 and one optional parameter, i.e. str3 with their default value. Here, the optional parameter uses this default value when we do not pass any value to the optional parameter and when we pass the value of the optional parameter in the method call, then it will take the passed value.
// C# program to illustrate how to create// optional parameters using default valueusingSystem;classGFG {// Method containing optional parameter// Here str3 is a optional parameter// with its default valuestaticpublicvoidmy_add(stringstr1,stringstr2,stringstr3 ="GeeksforGeeks"){Console.WriteLine(str1 + str2 + str3);}// Main methodstaticpublicvoidMain(){my_add("Welcome","to");my_add("This","is","C# Tutorial");}}chevron_rightfilter_noneOutput:WelcometoGeeksforGeeks ThisisC# Tutorial
- By using Method Overloading: You can implement optional parameters concept by using method overloading. In method overloading, we create methods with the same name but with the different parameter list. This method is not the pure way to implement an optional parameter, but you can achieve the optional parameter concept by using this method.
Example: Here, we have two methods with the same name but the parameter list of these methods is different, i.e. first my_mul method takes only one argument whereas second mu_mul method takes three arguments.
// C# program to illustrate how to create// optional parameters using method overloadingusingSystem;classGFG {// Creating optional parameters// Using method overloading// Here both methods have the same// name but different parameter liststaticpublicvoidmy_mul(inta){Console.WriteLine(a * a);}staticpublicvoidmy_mul(inta,intb,intc){Console.WriteLine(a * b * c);}// Main methodstaticpublicvoidMain(){my_mul(4);my_mul(5, 6, 100);}}chevron_rightfilter_noneOutput:
16 3000
- By using OptionalAttribute: You can implement optional parameters by using OptionalAttribute. To implement the optional parameter first you need to add System.Runtime.InteropServices namespace in your program, then creates an optional parameter using the Optional keyword enclosed in square brackets before the definition of the parameter in the method. The default value of OptionalAttribut is zero.
Example: Here, we create an optional parameter using OptionalAttribute, here num2 is an optional parameter in the my_mul method.
// C# program to illustrate how to create// optional parameters using OptionalAttributeusingSystem;usingSystem.Runtime.InteropServices;classGFG {// Method containing optional parameter// Using OptionalAttributestaticpublicvoidmy_mul(intnum,[ Optional ]intnum2){Console.WriteLine(num * num2);}// Main MethodstaticpublicvoidMain(){my_mul(4);my_mul(2, 10);}}chevron_rightfilter_noneOutput:0 20
- By Params Keyword: You can implement optional parameters by using the params keyword. It allows you to pass any variable number of parameters to a method. But you can use the params keyword for only one parameter and that parameter is the last parameter of the method. This method is not the pure way to implement an optional parameter, but you can achieve the optional parameter concept by using this method.
Example: Here, optional parameter is created using the params keyword. Here a1 is the optional parameter and in which you can pass from zero to up to any number of variables.
// C# program to illustrate how to create// optional parameters using params keywordusingSystem;classGFG {// Method containing optional// parameter Using params keywordstaticpublicvoidmy_mul(inta,paramsint[] a1){intmul = 1;foreach(intnumina1){mul *= num;}Console.WriteLine(mul * a);}// Main methodstaticpublicvoidMain(){my_mul(1);my_mul(2, 4);my_mul(3, 3, 100);}}chevron_rightfilter_noneOutput:1 8 900
Recommended Posts:
- C# | Optional Parameters
- Out Parameter With Examples in C#
- Different ways to create an Object in C#
- Different Ways to Take Input and Print a Float Value in C#
- Different ways to convert String to Integer in C#
- Different Ways to Convert Double to Integer in C#
- Different ways to sort an array in descending order in C#
- Different Ways to Convert Char Array to String in C#
- How to make a balloon ToolTip window in C#?
- Difference between Method Overriding and Method Hiding in C#
- C# | Method Overloading
- C# | IsNullOrEmpty() Method
- C# | Math.Tan() Method
- Method Hiding in C#
- C# | Math.Cos() Method
- C# | Math.Sin() Method
- C# | Math.Pow() Method
- C# | IndexOfAny() Method
- Main Method in C#
- C# | Dictionary.Add() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

