This is a short lesson that covers an interesting feature of methods in C# and that is the allowance of optional parameters. The “optional” part refers to whether or not the values for the parameter are supplied upon calling the method. If the programmer decides not to supply any values for the parameter, it then defaults to a value assigned in the method signature itself.

Step 1: Create a New Project**

Refer to where we left off with the previous lesson and use that as the starting-point for this one, and call the project “CS-ASP_032”. And in the existing performAttack() method modify it to include another optional input parameter:

cs-asp-032---creating-optional-parameters.001

Step 2: Using Optional Parameter’s for Default Values

Notice how this input parameter includes a value already assigned to it, determined before the method is even called. Also, notice that our existing calls to this method still work even though they are missing this input parameter. This is because the optional parameter we created defaults to the value if no other is given when the method is called. The other (non-optional) parameters are absolutely required and the compiler would make sure you include them otherwise it will produce an error:

cs-asp-032---creating-optional-parameters.002

The way this works here is criticalHitChance automatically takes on the value defined in the method signature unless it is explicitly overridden with a different value supplied at the method call. You will be able to identify optional parameters with Intellisense, which shows them wrapped in square brackets, along with their default value:

cs-asp-032---creating-optional-parameters.003

Step 3: Overriding the Optional Parameter’s Default Value

Now here is the performAttack() method with the optional parameter designated at the method call:

cs-asp-032---creating-optional-parameters.004

You can add as many optional parameters as you want but they must be appended after the non-optional input parameters:

cs-asp-032---creating-optional-parameters.005

cs-asp-032---creating-optional-parameters.006

Step 4: Constraints When Omitting Optional Parameter Arguments

Now, let’s see what happens if we want to omit the first optional parameter at the method call:

cs-asp-032---creating-optional-parameters.007

You will notice that the compiler doesn’t let you do this and that’s because the only parameter that is truly optional, in all cases, is the last optional parameter within the sequence. So, you can safely omit this last optional parameter, even if you choose to use the optional parameters that came before it:

cs-asp-032---creating-optional-parameters.004

Tip:

You may have noticed that we didn’t actually reference the optional parameters in the body of the performAttack() code block. Obviously you would want to make use of all of your input parameters in a real-world code project, however, this example is simply for the purpose of demonstrating how optional parameters work.


Lessons in this Course