concept optional parameter in category c#

appears as: optional parameters, n optional parameter, The optional parameters, optional parameters, An optional parameter, optional parameter, optional parameter
C# in Depth, Fourth Edition

This is an excerpt from Manning's book C# in Depth, Fourth Edition.

I’m not going to go into the details of how generic type inference works. It’s changed a lot over time as the language designers figure out ways of making it work in more cases. Overload resolution and type inference are closely tied together, and they intersect with all kinds of other features (such as inheritance, conversions, and optional parameters in C# 4). This is the area of the specification I find the most complex,[4] and I couldn’t do it justice here.

The other features in C# 4 also improve interoperability, particularly with COM. Some improvements are specific to COM, such as named indexers, implicit ref arguments, and embedded interop types. Optional parameters and named arguments are useful with COM, but they can also be used in purely managed code. These two are the features from C# 4 that I use on a daily basis.

Listing 4.15. Calling a method with optional parameters
static void Method(int x, int y = 5, int z = 10)       #1
{
    Console.WriteLine("x={0}; y={1}; z={2}", x, y, z); #2
}

...

Method(1, 2, 3);                                       #3
Method(x: 1, y: 2, z: 3);                              #3
Method(z: 3, y: 2, x: 1);                              #3
Method(1, 2);                                          #4
Method(1, y: 2);                                       #4
Method(1, z: 3);                                       #5
Method(1);                                             #6
Method(x: 1);                                          #6
  • C# 4 allows parameters to specify default values. Any parameter with a default value is an optional parameter and doesn’t have to be provided by the caller.
  • C# 4 allows arguments to specify the name of the parameter for which it’s intended to provide the value. This works with optional parameters to allow you to specify arguments for some parameters but not others.
  • C# in Depth

    This is an excerpt from Manning's book C# in Depth.

    Sometimes you don’t want to tell a method everything it needs to know, such as when you almost always use the same value for a particular parameter. Traditionally the solution has been to overload the method in question, but C# 4 introduced optional parameters to make this simpler.

    Figure 13.1. Declaring optional parameters
    Listing 13.1. Declaring and calling a method with optional parameters
    static void Dump(int x, int y = 20, int z = 30)  #1 {    Console.WriteLine("x={0} y={1} z={2}", x, y, z); } ... Dump(1, 2, 3); #2 Dump(1, 2); #3 Dump(1);  #4 #1 – 1 Declares method with optional parameters #2 – 2 Calls method with all arguments #3 – 3 Omits one argument #4 – 4 Omits two arguments !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!

    The optional parameters are the ones with default values specified . If the caller doesn’t specify y, its initial value will be 20, and likewise z has a default value of 30. The first call explicitly specifies all the arguments; the remaining calls ( and ) omit one or two arguments, respectively, so the default values are used. When there’s one argument missing, the compiler assumes that the final parameter has been omitted, then the penultimate one, and so on. The output is as follows:

    x=1 y=2 z=3
    x=1 y=2 z=30
    x=1 y=20 z=30

    Note that although the compiler could use some clever analysis of the types of the optional parameters and the arguments in order to work out what’s been left out, it doesn’t: it assumes that you’re supplying arguments in the same order as the parameters.[2] This means that the following code is invalid: