Passing Optional and Named Parameter

Learn about utilizing optional parameters in C#, assign default values for optional parameters, and use named parameters.

By utilizing optional parameters in C#, we can enhance the versatility of our methods. Assigning default values within the method parameter list allows us to make certain parameters optional, allowing for more flexible method calls. These optional parameters must always be placed at the end of the parameter list. Additionally, named parameters enable us to pass values in a different order than they were declared, further enhancing the usability and readability of our code.

Passing optional parameters

Another way to simplify methods is to make parameters optional. We make a parameter optional by assigning a default value inside the method parameter list. Optional parameters must always come last in the list of parameters. We will now create a method with three optional parameters:

Step 1: In Person.cs, add statements to define the method, as shown in the following code:

Get hands-on with 1400+ tech skills courses.