Sunday, August 15, 2010

Dynamic Type Visual Studio 2010

With the release of the visual studio 2010 there are new feature introduced in the frame work. One of which is the dynamic type.The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at run time. The dynamic type simplifies access to COM APIs such as the Office Automation APIs, and also to dynamic APIs such as IronPython libraries, and to the HTML Document Object Model (DOM).Type dynamic behaves like type object in most circumstances. However, operations that contain expressions of type dynamic are not resolved or type checked by the compiler. The compiler packages together information about the operation, and that information is later used to evaluate the operation at run time. As part of the process, variables of type dynamic are compiled into variables of type object. Therefore, type dynamic exists only at compile time, not at run time.
C# was until now a statically bound language. This means that if the compiler couldn't find the method for an object to bind to then it will throw compilation error.We declare at compile-time the type to be dynamic, but at run-time we get a strongly typed object.Dynamic objects expose members such as properties and methods at run time, instead of compile time. If you try to access members of a Dynamic type in visual studio IDE you will get the message "The operation will be resolved at runtime".
Below is my sample code which is used to use the dynamic type. Here you can used the dynamic type in order to declare the object and I have assigned the value 10, as If you use the dynamic variable without assignment it will give you error at compile time. So I have assign the value 10 to the dynamic variable before  using the variable.

dynamic dynDynamic= 10; int intConversionFromDynamic = dynDynamic; Console.WriteLine("Dynamic Type: {0} And Value: {1} ", dynDynamic.GetType(), dynDynamic); dynDynamic = "Some String Value "; Console.WriteLine("Dynamic Type: {0} And Value: {1} ", dynDynamic.GetType(), dynDynamic); dynDynamic = 10.0; Console.WriteLine("Dynamic Type: {0} And Value: {1} ", dynDynamic.GetType(), dynDynamic); Console.WriteLine("Converted Type: {0} And Value: {1} ", intConversionFromDynamic.GetType(), intConversionFromDynamic); Console.ReadKey();

Below is the out of the above code. In the above code you can see that I have one dynamic variable and I have assign it different values integer , string and double. After assigning the new value I have output the type and the value which is stored in the dynamic variable.

I have also cast the initial value of the dynamic variable to the int variable, As initially I have store 10 which is of type int so I have used implicit casting here but if you want to assign the value 10 to the byte type then you have to use the Explicit cast to store the value to the byte type variable or int16 type variable. One thing to be noted if you access invalid property then it will throw exception, which will be very castly as you know. Hope you get some idea of how to use the dynamic type in your code. 

1- Msdn

All and any comments / bugs / suggestions are welcomed!

No comments: