If you find this article useful, consider making a small donation to show your support for this web site and its content.
DiscountASP
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.

Dynamic Typing in C# 4.0

by Farooq Kaiser 5. August 2009 07:55

C# 4.0 introduces a new static type called dynamic. When an instance variable is defined with the type dynamic, the compiler ignores the call and won't complain. These actions are picked up at runtime. Here are comparison with C# 3.0 and C# 4.0 using Dynamic Types.

Let's say I have the following code in C# 3.0

 // will not work in c# 3.0 
       object o = "farooq"; 
       Console.Write(o.Insert(o.Length, " Kaiser "));  
      
// will work in c# 3.0 
      object o = "farooq"; 
      string myName = o as string ; 
      Console.Write(myName.Insert(myName.Length, " Kaiser ")); 
//Now, using Dynamic Types in C# 4.0             
            // will work in c# 4.0 
       dynamic d = "farooq";       // implicit conversion 
       Console.Write(d.Insert(d.Length, " Kaiser "));  
       
//Let me explain more than the above code. 
       //will not work in Current c#. 
       List<var> oldList = new List<var>();
     
      //will work in dynamic types 
      List<dynamic> newList = new List<dynamic>();

Summary 

In this article, we explored C# 4.0’s new feature dynamic types and I will continue to explore C# 4's features in the next article.

     

 

Currently rated 3.0 by 7 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

.NET | C#

Comments

Jobs Autos Real estate Videos Power by Google
awesome comments