TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   C# 4.0 Dynamic (http://forums.techarena.in/software-development/1210750.htm)

switchblade327 09-07-2009 06:04 PM

C# 4.0 Dynamic
 
One of the core features introduced in C# 4.0 is called Dynamic Lookup which allows a unified approach to invoking things dynamically. C# 3.0 was all about Language Integrated Query (LINQ) and C# 4.0 is all about dynamic programming. C# 4.0 brings some of flexibility and declarative style of programming to C#. C# 4.0 features a new dynamic keyword that allows you to mix in a bit of late-bound code in the midst of your otherwise statically typed code. It is used as a data type in much the same way the var keyword is used. The biggest reason is that it allows a C# program to use dynamic dispatch to more naturally create objects coming from a dynamic language.

As an example, suppose you have a method like this:
Code:

public dynamic myMethod()
{
    // Get your work done here...
}

and you call it as:

Code:

dynamic callMethod = myMethod();
callMethod.Do("Your Job");

The C# 4.0 compiler allows you to call a method with any name and any arguments because callMethod is declared as "dynamic".

XSI 09-07-2009 06:14 PM

Re: C# 4.0 Dynamic
 
Does that means C# 4.0 is lacking the statically typed language? Because if you don't mention the datatype of a variable, there should be an error saying data type is missing. If C# 4.0 gives us so much of flexibility then no one will mention any data type and will simply use "dynamic" keyword.

switchblade327 09-07-2009 06:19 PM

Re: C# 4.0 Dynamic
 
Well, no. The basic idea behind this is to provide flexibility to write C# code which talks to objects (or APIs). The "dynamic" operand differs compile-time declaration with run-time. At run-time the "dynamic" is replaced with the actual datatype. So if a variable is assigned a value of "1", the datatype will automatically be converted to "System.Int32".


All times are GMT +5.5. The time now is 06:50 PM.