1.

What is dynamic keyword?

Answer»

DYNAMIC is a new static type that acts like a placeholder for a type not known until runtime. Once the dynamic object is declared, it is possible to call operations, get and set properties on it, even pass the dynamic instance pretty much as if it were any normal type.

The dynamic type enables the operations in which it occurs to BYPASS compile-time type checking. these operations are resolved at run time. 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.

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.

The following example contrasts a variable of type dynamic to a variable of type object. To verify the type of each variable at compile time, place the mouse pointer over dyn or obj in the WRITELINE statements. IntelliSense shows dynamic for dyn and object for obj.

class Program {    static void MAIN(string[] args)    {        dynamic dyn = 1;        object obj = 1;        // Rest the mouse pointer over dyn and obj to see their        // types at compile time.        System.Console.WriteLine(dyn.GetType());        System.Console.WriteLine(obj.GetType());    } }

Limitations of dynamic

  •  Extension methods cannot be called
  •  Anonymous functions (lambdas) cannot be called
  •  Because of the above, LINQ support is limited


Discussion

No Comment Found