- using System;
-
- namespace OptionalParameter
- {
- class Program
- {
- public static void Sum(int firstNumber, int secondNumber, params object[] restOfTheNumbers)
- {
- int result = firstNumber + secondNumber;
- foreach (int i in restOfTheNumbers)
- {
- result += i;
- }
-
- Console.WriteLine("Total = " + result.ToString());
- }
-
- public static void Main()
- {
-
- Sum(10, 20);
-
-
- Sum(10, 20, 30, 40, 50);
- }
- }
- }
The Sum method allows the user to add 2 or more numbers. The firstNumber and secondNumber parameters are mandatory, whereas the MoreNumbers parameter is optional.
Output
Note: A parameter array must be the last parameter in a formal parameter list. The following method will not compile and gives a compile time error.
- public static void Sum(int firstNumber, params object[] MoreNumbers,
- int secondNumber)
- {
-
- }
2. Using method overloading
- using System;
-
- namespace OptionalParameter
- {
- class Program
- {
- public static void Sum(int firstNumber, int secondNumber, int[] MoreNumbers)
- {
- int result = firstNumber + secondNumber;
- if (MoreNumbers != null)
- {
- foreach (int i in MoreNumbers)
- {
- result += i;
- }
- }
- Console.WriteLine("Sum = " + result);
- }
- public static void Sum(int firstNumber, int secondNumber)
- {
- int result = firstNumber + secondNumber;
- Console.WriteLine("Sum = " + result);
- }
- public static void Main()
- {
-
- Sum(10, 20);
-
-
- Sum(10, 20, new int[]{30,40,50});
- }
- }
- }
In the preceding example I have two Sum methods that are overloaded or we can say there are 2 versions of the Sum method.
Output
3. Parameters optional by specifying parameter defaults
- using System;
-
- namespace OptionalParameter
- {
- class Program
- {
- public static void Sum(int firstNumber, int secondNumber, int[] MoreNumbers=null)
- {
- int result = firstNumber + secondNumber;
- if (MoreNumbers != null)
- {
- foreach (int i in MoreNumbers)
- {
- result += i;
- }
- }
- Console.WriteLine("Sum = " + result);
- }
- public static void Main()
- {
-
- Sum(10, 20);
-
-
- Sum(10, 20, new int[]{30,40,50});
- }
- }
- }
In the preceding example I have set MoreNumbers=null by default and when I call Sum(10,20) then by default the value passed to MoreNumber=Null and when I call the same method like Sum(10,20, new int[]{30,40,50}) then it changes the default value.
Output
Example 2:
- using System;
-
- namespace OptionalParameter
- {
- class Program
- {
- public static void Test(int a,int b=5,int c=10)
- {
- Console.WriteLine("a = " + a);
- Console.WriteLine("b = " + b);
- Console.WriteLine("c = " + c);
- }
- public static void Main()
- {
- Test(3);
- Console.WriteLine();
- Test(2, 3);
- Console.WriteLine();
- Test(5, 4, 6);
- }
- }
- }
In the preceding example the
Test method contains the 3 parameters a, b, c but we have set b=5 and c=10 so here a parameter will be a required parameter and b and c are optional parameters.
Note: Optional parameters must appear after all the required parameters, in other words the following method will not compile.
Assigning a value to a specific optional parameter
The following sample shows how to assign a value to a specific optional parameter:
Output
4. Making method parameters optional using OptionalAttribute
In this example we will discuss
OptionalAttribute in the
System.Runtime.InteropServices namespace.
- using System;
- using System.Runtime.InteropServices;
-
- namespace OptionalParameter
- {
- class Program
- {
- public static void Sum(int firstNumber, int secondNumber, [Optional] int[] MoreNumbers)
- {
- int result = firstNumber + secondNumber;
- if (MoreNumbers != null)
- {
- foreach (int i in MoreNumbers)
- {
- result += i;
- }
- }
- Console.WriteLine("Sum = " + result);
- }
- public static void Main()
- {
-
- Sum(10,20);
-
-
- Sum(10, 20, new int[] { 30, 40, 50 });
- }
- }
- }
In the preceding example we have a function Sum where two parameters are required, firstNumber and secondNumber, but the last parameter MoreNumber I have marked as an [
Optional] attribute.
Output
Thank you. Happy Coding :)