C# Programming: Language Enhancements in C# 4.0

Introduction

As we saw the features of Microsoft Visual Studio 2010 and ASP.NET 4.0 in my previous articles we will look into the new language enhancements in C# 4.0 in this article. 4.0 is the version of C# language that is bundled along with the .NET framework 4.0. There is a 1 hour video available on channel 9 in which the C# 4.0 design team discusses its new features here. Also you should check Microsoft’s website called CSharpFuture where you could find many tutorials and code samples.

Dynamic Nature of C# 4.0

If you closely look at how C# language is being evolved during the recent times then you will realize the fact that it is moving slowly to the dynamic side from its static type checking nature. A good example would be the introduction of generics in C# 2.0, anonymous types, etc. Now in the 4.0 version there is a great deal of support for dynamic programming.

For example in the dynamic programming it is left to the runtime to infer the type of an object and process it accordingly. This offers a good amount of flexibility to the language. The only drawback could be since the actual type checking is done during the runtime you are at a higher risk of getting runtime exceptions. So should be extremely careful while coding.

This dynamic inferring of types at runtime is made possible in C# 4.0 because of a component called Dynamic Language Runtime (DLR) which is a part of the .NET framework 4.0.

Dynamic Keyword

There is a new keyword for specifying the member type in C# 4.0 called ‘dynamic’. As the name indicates the type of a dynamic object is not static and it is totally dynamic. You can assign object of any type to the dynamic variable which will be inferred at the runtime and executed accordingly. The best part is that in the dynamic objects you can make use of the methods, properties or indexers dynamically. To me this has really a big advantage for the developers since they don’t have to use different variables for different types.

Let us check the program below


namespace CSharp4Samples
{
   class Program
   {
       static void Main(string[] args)
       {
           //declare adynamic object and assign a generic list
           dynamic dynamicObject = GetGenericList();
           //dynamically adds a string
           dynamicObject.Add(“codeguru.com from generic list”);
           //dynamically access the indexer
           Console.WriteLine(“The dynamic object value is: {0}”, dynamicObject[0]);

           //now assign a array list
           dynamicObject = GetArrayList();
           //add the string dynamically
           dynamicObject.Add(“codeguru.com from array list”);
           //dynamically access the indexer of array list
           Console.WriteLine(“The dynamic object value is: {0}”, dynamicObject[0]);

           Console.ReadKey();
       }

       /// <summary>
       /// Gets a generic list object
       /// </summary>
       /// <returns></returns>
       private static dynamic GetGenericList()
       {
           List<string> list = new List<string>();
           return list;
       }

       /// <summary>
       /// Gets a array list object
       /// </summary>
       /// <returns></returns>
       private static dynamic GetArrayList()
       {
           ArrayList list = new ArrayList();
           return list;
       }
   }
}


In the above code check how the objects of an array list and a generic list are stored in the same variable dynamicObject which is of type dynamic. Also see the fact that the Add method and the indexers are being accessed so dynamically. As I said earlier in the dynamic approach you should be very careful since there is a good chance of doing a mistake which will go unnoticed during compile time.

Fig 1.0 shows the output window



Fig 1.0

In languages like IronPython and Ruby the dynamic objects will extensively implement the interface IDynamicObject. So in a C# application if the dynamic object implements IDynamicObject then the DLR will leave the whole operation to the dynamic object itself.

More by Author

Must Read