- Overview and optional arguments: summary of the article and explanation of named and optional - default values - in method’s arguments.
- Method parameter: how to handle a variable number of arguments.
- Extension methods: extending or adding methods for existing types.
- Generic constraints on type parameters: exposition in the usage of constraints to generic parameters in methods.
Named and Optional arguments
C# 4.0 added the feature of named and optional arguments; method overloading is no longer necessary to handle default values for arguments when calling methods. Now, through the method’s signature, we can indicate to the framework the aim to have a value - in any given parameter - when omitted.
In the following method, which “draws” a circle, we defined that the radius will have a preset value of 1.

If we make the following calls to the method we will get the Output shown.


Notice that the second call only contains two arguments, so the framework sets the radius variable to 1, as we can validate in the output messages.
It’s important to mention that in the signature all required parameters must precede the optional ones.
The named arguments, is another feature which is, to a certain degree, related to optional arguments. In our previous example, let’s change the method’s signature to have a default value set to 0 for x and y arguments as follows.

With this change, we may be able to call the method without arguments and the result would be a circle centered at (0, 0) with a radius of 1. Besides this possibility, we can call the method with any argument, and we can omit the ones we would like to use with default values. For instance if we would like to have a circle with center in (0, 10) and radius 1, we can add to our code a call like this:

The resulting output would be the following:

In the next part we will explain parameters with variable number of arguments.