C# 4.0 New Features - Straight to the Point (Part 1)

The new features introduced in the C# in the .NET Framework 4.0 are as in the following:

  1. Optional Parameters
  2. Named Parameters
  3. Overload Resolution
  4. Covariance and Contravariance
  5. Dynamic
  6. 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:

Csharp-4.0-new-features1.jpg

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:

Csharp-4.0-new-features2.jpg

  • 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:

Csharp-4.0-new-features3.jpg

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.

Csharp-4.0-new-features4.jpg
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:

Csharp-4.0-new-features5.jpg

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.

Csharp-4.0-new-features6.jpg
We are passing now the value of parameter "b" first and then "a".  Now check the output.

Csharp-4.0-new-features7.jpg

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.

Csharp-4.0-new-features8.jpg

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.

Csharp-4.0-new-features9.jpg

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:

Csharp-4.0-new-features10.jpg

Csharp-4.0-new-features11.jpg

Now let us the overloading method which accepts the parameter of type double and see how the compiler resolves the method execution.

Csharp-4.0-new-features12.jpg

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.

Csharp-4.0-new-features13.jpg

We see the new method is not executed. Unless you pass the double value, it will not be executed. Let us check that.

Csharp-4.0-new-features14.jpg

Run the program and see the output:

Csharp-4.0-new-features15.jpg

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. 


Recommended Free Ebook
Similar Articles