.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

C# 4.0 Dynamic Typing

Posted By:Baimey Rajesh       Posted Date: August 26, 2012    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

Dynamic is a new type introduced in c# 4.0.Dynamic types are declared with the dynamic keyword.

Dynamic typing (dynamic type)

Dynamic is a new type introduced in c# 4.0.Dynamic types are declared with the dynamic keyword.

C# was until now a statically bound language. now the story changes, with the introduction of Dynamic Keyword.

In declarations, as the type of a property, field, indexer, parameter, return value, local variable, or type constraint. 

// A dynamic field. 
static dynamic field;
// A dynamic property.
dynamic prop { get; set; }

If you try to access members of a Dynamic type in visual studio IDE you will get the message "The operation will be resolved at runtime".

Type dynamic behaves like type object in most circumstances. At compile time, an element that is typed as dynamic is assumed to support any operation.

In explicit type conversions, as the target type of a conversion,

dynamic d;
//type conversion
int i = 20;
d = (dynamic)i;
string s = "Example string.";
d = (dynamic)s;
DateTime dt = DateTime.Today;
d = (dynamic)dt;

When we assign a value,

dynamic d1 = 7;
dynamic d2 = "a string";
dynamic d3 = System.DateTime.Today;
dynamic d4 = System.Diagnostics.Process.GetProcesses();

The following sample may make things clear.

dynamic num = 1;

dynamic str = “Baimey”;

Console.WriteLine("Your number is " + num);

Console.WriteLine("Your string is " + str);


In the first case u get a number output and later in the second option it is string.


Dynamic Type is a nice feature when it comes to interoperatibility and .NET usage with other Dynamic languages.





 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend