Sunday, May 11, 2014

Optional Parameters and Named Parameters in C#

I just don't like it when simple things are explained in a complex manner. So let me break it down for other C# noobs, in case you have a textbook like mine that provides an insufficient explanation or if you just don't understand these two types of parameters.

Optional Parameters

You will be surprised to learn that, as the name suggests, this means the parameters* are optional. The client code does NOT have to provide arguments for these parameters. Why? Because a default value is already included for you.

Example: public void optParam (int x, int y =2, int z = 3)
                       {some code};//this method has two optional parameters

In this case, there are two optional parameters, "y" and "z". Parameter "y" has a default value of 2 and parameter "z" has a default value of 3. If no value is provided for "y" or "z", they will retain their default values. You will notice that parameter "x" has no default value provided. This means that the client code MUST provide an argument for this parameter. Also be aware that the required parameter must come before the optional parameters.

Obviously, there is a problem. If you want to provide an argument for "z" and not for "y", how can you make the arguments be evaluated so that the first argument will be applied to "x" and the second argument to "z"? You can't. As MSDN informs us, you would have no choice but to specify the argument for the optional parameter "y" before providing the argument for "z". Needless to say, there would be no point in having an optional parameter "y" in that case. This is where named parameters come to the rescue.

*Per MSDN, optional and named parameters can be used with methods, constructors, indexers and delegates.

Named Parameters

When calling the method, the client code need only name the optional parameters for which it wishes to provide a value, avoiding the problem just described.

Example:  public void optParam (int x, int y =2, int z = 3) {some code};
                optParam (20, z: 30);

This result is x = 20, y =2 and z = 30. You avoid having to rewrite the default value and another benefit is that you can put the named parameters in any order. The same rule about required parameters coming before optional parameters applies here. The arguments supplied for the optional parameters can come in any order as to each other, but they must come after the arguments for the required parameters.

Example:  public void optParam (int x, int y =2, int z = 3, int zz = 5) {some code};
                optParam (20, zz: 30, y: 10);//okay
                optParam (y: 30, zz: 10, 20);//not okay

Here you can see a sample of code I wrote to show how the two types of parameters work together and a screen shot of the results: https://github.com/htravar/Parameters. You may also find this article helpful.

This topic does get more complex, but I hope I've explained the basics in a basic way. Enjoy the parameter shortcut.

No comments:

Post a Comment