15 June 2011

New in framework 4 - dynamic object

Below the previous post on framework 4 (Parallel) - time will demonstrate the new type object - "dynamic".

Dynamic object is built at runtime.
Unlike the previous version of the framework, when we received the object VAR - VAR built during Compilation, so that in fact we knew the type of object VAR in advance.
Dynamic does not know it's like you until it actually materialize.

Let's see an example


Please note that varObject already during Compilation does not give us continue to work because of that string is not int.
However, dynamicObject passes Compilation.


Here is the correct code - passing Compilation:

using System;
using
 System.Collections.Generic;
using
 System.Linq;
using
 System.Text;
namespace
 ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var varObject = 1;
            Console.WriteLine("var object = {0}. the type is {1}",varObject,varObject.GetType());
            //var varObject = "1";
            //Console.WriteLine "var object = {0}. the type is {1}", varObject, varObject.GetType());
            dynamic dynamicObject = 1;
            Console.WriteLine("dynamic object = {0}. the type is {1}", dynamicObject, dynamicObject.GetType());
            dynamicObject = "1";
            Console.WriteLine("dynamic object = {0}. the type is {1}", dynamicObject, dynamicObject.GetType());
            Console.ReadLine();
        }
    }
}

Yours,
Roi

No comments:

Post a Comment