New features of C# 4.0


Introduction


Today with the new release of Visual Studio.NET 2010 and Microsoft.NET Framework 4.0, we have new features added to the C# language. In this article, I want to talk about the new features being integrated with the new version of the language C# 4.0.

Here is a list of new features added to C# 4.0.
  • Dynamic programming
  • Named and optional parameters
  • Covariance and Contravariance
Dynamic Programming
The first feature in C# 4.0 is the support to dynamic programming. A new keyword is introduced: dynamic and it's used to tell the compiler that the object is defined at runtime, and it's able to response to dynamic messages. A dynamic object is assumed to support any operation at compile time; only at runtime you will get an error.

This is very important for accessing code created on non-typed languages such as Python and Ruby. For example, let's supposed we want to instantiate an object whose definition is created on Iron-Python (the .NET-enabled version of Python), you can send messages to the object by using the code in Listing 1.

dynamic objCust = GetCustomer();
objCust.FirstName = "John";
objCust.LastName = "Olamendy";
objCust.CalculateSalary();




Listing 1

In this case, the variable objCust is declared dynamic, in this way, saying to the C# compiler that the type of Customer is not known until run time, but it can dispatch messages to it. For example, at run time, the compiler discovers the properties to be called (FirstName and LastName) and the method CalculatedSalary which is resolved only on execution. This way, the dynamic run time receives information about the call such as its name and parameters. With this information, our application can call the right function (in Iron-Python), and the program can continue. If the method is not defined, then an exception of type RuntimeBinderException is thrown.

With this new feature, C# becomes more useful as a language that can connect to statically typed as well as dynamically executed languages. In the new version of C#, this feature will be used to provide special ways to handle XML and SQL data.

Another use of dynamic variables (although dangerous, and I don't recommend to use it), it's to reuse a variable for different types of data (see Listing 2).

dynamic foo = 1234567890;
System.Console.WriteLine(foo);
foo = "John Charles";
System.Console.WriteLine(foo);
foo = true;
System.Console.WriteLine(foo);

Listing 2

Named and Optional Parameters
Another new feature is named and optional parameters. Optional parameters allow giving a method parameter a default value, so you don't have to specify it every time you call the method (see Listing 3).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpNewFeatures
{
    class Program
    {
        public static void Method(int age, String firstname="John"double salary=4000.99)
        { }
        static void Main(string[] args)
        {
            Method(30); //The same as Method(30,"John",4000.99)
            Method(30, "Mary"); //The same as Method(30,"Mary",4000.99)
            Method(30, "Mary", 2000.99);
        }
    }
}

Listing 3

Using named parameter, a developer can skip parameters that have default values, and name only the parameters that have non-default values.

For example if you write the following code in the Listing 4, you will get an error because the second parameter's type, in the method invocation, does not match with a String data type.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpNewFeatures
{
    class Program
    {
        public static void Method(int age, String firstname="John"double salary=4000.99)
        { }
        static void Main(string[] args)
        {
            Method(30, 2000.9);
        }
    }
}

Listing 4

In order to solve this problem, we're going to use named parameters as shown in the Listing 5. You can see that the syntax is very simple. Just call the parameters by [parameter_name]:[parameter_value].

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpNewFeatures
{
    class Program
    {
        public static void Method(int age, String firstname="John"double salary=4000.99)
        { }
        static void Main(string[] args)
        {
            Method(30, salary:2000.9);
        }
    }
}

Listing 5


Covariance and Contravariance

Another new feature is the use of covariance and contravariance of generics. Covariance allows casting of generic types to the base types, for example, IEnumerable will be implicitly convertible an IEnumerable if A can implicitly be converted to B (see Listing 6).



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpNewFeatures
{
    class Program
    {
        static void Main(string[] args)
        {
            // Say we have a list of strings
            IList<string> arrNames = new List<string>();
            // Now we can covert it into an Enumerable collection
            IEnumerable<object> objects = arrNames;
        }
    }
}

Listing 6

And the final feature added to C# is the improvement to COM interoperability. A lot of changes to facilitate the COM interoperability have been added such as passing values to references parameters.

Conclusion

In this article, I've covered the new features added to the C# language version 4.0. This new version of C# has come with Visual Studio.NET 2010 and Microsoft.NET 4.0.

Popular Posts