The new features introduced in the C# in the .NET Framework 4.0 are as in the following:
- Optional Parameters
- Named Parameters
- Overload Resolution
- Covariance and Contravariance
- Dynamic
- New Compiler options.
These are some of the new features that were missing in the previous version. This article discusses straight to the point on what they are with an example with comparison to the older versions.
Create a C# console application in Visual Studio 2010 for testing these examples.
Optional Parameters
Optional Parameters help you to omit passing values to some of the parameters of a method or a constructor. The optional parameter(s) should always appear after the required parameters.
Write a method called MyMethod(int a=70, int b) and you will see the following message:
So, we need to make the parameter "b" to be optional to make the parameter "a" to be optional. Now modify the method and see if the program compiles. Now call the method in the Main method:
- If you do not pass parameters, the method still executes with the default parameters.
- If you pass only one parameter, the method assumes that you passed the value for the first optional parameter.
To check the preceding two points run the program and see the output:
Note: There will be no performance issues in using the Optional Parameters. Basically, the optional parameters have been introduced to have compatibility with Microsoft Office components. Also, though we are not specifying the keyword "Optional" as we do in VB.NET, the compiler still builds the required internal code to specify that the parameter is to be treated as optional.
Named Parameters
We are going to take the same example to discuss Named Parameters. Named parameters are introduced in .NET 4.0 for C# for facilitating the developers to specify which optional parameter the value is passed from the calling code.
In the above example, when we passed the value 90 to the method MyMethod, it assumed the value is for the first parameter "a". What if we wanted to pass value for "b", then the Named Parameters are used. One more advantage is that you need not pass the parameters in the order of how they have been defined in the signature of the method.
Here we are explicitly specifying that we are passing a value for parameter "b" and not for "a". We can check this from the output:
Now you find that the value 90 is not assigned to parameter "a", but to "b".
Let us see how we can reorder the passing of values to the method with named parameters.
We are passing now the value of parameter "b" first and then "a". Now check the output.
You will find the parameter values are properly assigned.
Overload Resolution
Now let us have two overloading methods and check how the system resolves which method to call.
There are two methods which the integer parameter "x" with the second method having an optional parameter "y". In theory, since the parameter "y" is optional it is possible that the compiler can match the second method too when we pass only value in the calling method.
Let us run the program and see how the compiler resolves the overloading.
Now you see that for value=90, it has chosen the first overloading method. When we passed two values then it has only selected the second method for execution.
What happens when we make the parameter "x" also as optional and execute the program? It must still the same. Let us check the output:
Now let us the overloading method which accepts the parameter of type double and see how the compiler resolves the method execution.
Run the program to see if it executes the selected method or it will execute the code of the method which accepts an integer parameter.
We see the new method is not executed. Unless you pass the double value, it will not be executed. Let us check that.
Run the program and see the output:
Now you can find that the overloaded methods with a parameter of type double. Let us discuss the other 3 features in the next article.