Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is an Optional parameter in C#?
By default, all parameters of a method are required. However, C# allows you to define optional parameters that do not force you to pass arguments at calling time. This means you can call a method without passing values for some parameters.
Optional parameters contain default values in the function definition. If you do not pass an argument for an optional parameter at calling time, the default value is used automatically.
There are different ways to make a parameter optional in C#.
Syntax
Following is the syntax for declaring optional parameters using default values −
public void MethodName(int required, int optional = defaultValue) {
// method body
}
Following is the syntax using the Optional attribute −
using System.Runtime.InteropServices;
public void MethodName(int required, [Optional]int optional) {
// method body
}
Using Default Value
The most common way to create optional parameters is by assigning default values directly in the method declaration −
Example
using System;
namespace DemoApplication {
class Demo {
static void Main(string[] args) {
OptionalMethodWithDefaultValue(5);
// Value2 is not passed as it is optional
OptionalMethodWithDefaultValue(5, 10);
// Value2 is passed
}
public static void OptionalMethodWithDefaultValue(int value1, int value2 = 5) {
Console.WriteLine($"Sum is {value1 + value2}");
}
}
}
The output of the above code is −
Sum is 10 Sum is 15
In the above example, the method OptionalMethodWithDefaultValue has value2 with a default value of 5. When no argument is passed for value2, it uses the default value 5. When an argument is provided, it overrides the default value.
Using Optional Attribute
The [Optional] attribute from System.Runtime.InteropServices can also be used to mark parameters as optional −
Example
using System;
using System.Runtime.InteropServices;
namespace DemoApplication {
class Demo {
static void Main(string[] args) {
OptionalMethodWithAttribute(5);
OptionalMethodWithAttribute(5, 10);
}
public static void OptionalMethodWithAttribute(int value1, [Optional]int value2) {
Console.WriteLine($"Sum is {value1 + value2}");
}
}
}
The output of the above code is −
Sum is 5 Sum is 15
Here the [Optional] attribute specifies that value2 is optional. When not provided, it defaults to the type's default value (0 for integers).
Key Rules for Optional Parameters
Optional parameters must always be specified at the end of the parameter list. Required parameters cannot appear after optional parameters −
Invalid Example
// This will cause a compilation error
public static void InvalidMethod(int value1 = 5, int value2) {
// Error: Optional parameters must appear after all required parameters
}
Multiple Optional Parameters
Example
using System;
namespace DemoApplication {
class Demo {
static void Main(string[] args) {
DisplayInfo("John");
DisplayInfo("Alice", 25);
DisplayInfo("Bob", 30, "Engineer");
}
public static void DisplayInfo(string name, int age = 18, string profession = "Student") {
Console.WriteLine($"Name: {name}, Age: {age}, Profession: {profession}");
}
}
}
The output of the above code is −
Name: John, Age: 18, Profession: Student Name: Alice, Age: 25, Profession: Student Name: Bob, Age: 30, Profession: Engineer
Conclusion
Optional parameters in C# provide flexibility by allowing method calls with fewer arguments. You can create them using default values or the [Optional] attribute. Remember that optional parameters must always appear after required parameters in the method signature.
