C# 4.0 : Dynamic Objects

C# 4.0 : Dynamic Objects

  • Comments 2

“Working with dynamic objects statically” – Anders Hejlsberg

When you work with C# 4.0 you get to work with many unmanaged world “dynamically”. What does that mean to you? It means you do not do things “statically” as you used to do in C# or in VB.NET. There are many benefits in static programming like statement completion, compile time checking, refactoring etc. But when it comes to interact with other types (those are unmanaged) you have to take chances by creating wrapper or call the native methods in parameters (by calling “Invoke” method and pass the name in string).

What is the benefit?

This will extend the power of static programming for any dynamic programming language like Iron Python or Ruby, even Silverlight or JavaScript.

In C# 4.0 you can statically type the dynamic objects. How that happens,

When you say ,

int i = 10;

var j = 11;

You are statically mentioning that j is of type int. And this gives you intellisense and you cannot do things even at the design time which are not allowed. This will even prevent you to build the assembly.

But when you say something like this,

dynamic i = 10;

dynamic d = 12.9;

dynamic s = "Hello";

it means you are “statically declaring dynamic” objects. What does that really mean?

Ø  The objects will be deferred at run-time (not in compile time, unlike “var”)

Ø  At the runtime all of them will be substituted for dynamic

Ø  Any result of any dynamic operation is again “dynamic”.

Now if we write the below block to check-out what is happening around,

static void Main(string[] args)

{

   dynamic i = 10;

   dynamic d = 12.9;

   dynamic s = "Hello";

 

   Console.WriteLine(i.GetType());

   Console.WriteLine(d.GetType());

   Console.WriteLine(s.GetType());

}

This will give you the below output,

 

System.Int32

System.Double

System.String

 

Below I have created a class called “DynaClass” and it has got few overloaded methods with different types of parameters. Here I will demonstrate how the run-time resolution would happen for the keyword “dynamic”.

public static class DynaClass

{

public static string CallMe(string i)

{

          return "You have called method for String";

}

 

public static string CallMe(int i)

{

          return "You have called method for Int";

}

 

     public static string CallMe(double i)

{

          return "You have called method for Double";

}

 

public static string CallMe(DateTime i)

{

return "You have called method for DateTime";

}

}

  

Now if you call the method through this code block,

//Call for Int

dynamic x1 = 10;

Console.WriteLine(DynaClass.CallMe(x1));

 

//Call for Double

dynamic x2 = DateTime.Now;

Console.WriteLine(DynaClass.CallMe(x2));

 

//Call for String

dynamic x3 = "Hello Dynamic";

Console.WriteLine(DynaClass.CallMe(x3));

 

//Call for DateTime

dynamic x4 = DateTime.Now;

Console.WriteLine(DynaClass.CallMe(x4));

Will give you the below output,

 

You have called method for Int

You have called method for DateTime

You have called method for String

You have called method for DateTime

 

As I said earlier that dynamically called method returns value dynamically. So the method CallMe will give you dynamic not “string” which will be resolved at run-time.

Namoskar!!!

Leave a Comment
  • Please add 8 and 2 and type the answer here:
  • Post
Page 1 of 1 (2 items)