Written by:2009/08/12 10:01 AM
I’ve been testing and evaluating C# 4.0, Visual Studio 2010 and the new .NET 4.0 over the last few weeks. One of the new features that I have noticed is the new Dynamic Type in C#. I have been in programming for many years and cut my teeth on dynamic languages such as dBASE and PHP. I know full well the pitfalls of dynamic languages as well as the immense freedom and speed of coding when using a dynamic language.
So what's up with the dynamic keyword, and what type was it exactly? Isn’t C# a type static language. Well the answer to that is that the dynamic keyword is ac
Dynamic type and the dynamic keyword are some of C#'s new features which are tied into Microsoft's new Dynamic Language Runtime (DLR) environment—a new feature of the .NET Framework that enables dynamic languages to reside side-by-side and interoperate with statically typed languages.
To support dynamic variable declaration in C# and .NET we have to use the new var and dynamic keywords. You do not have to specify the data type on the left hand-side of an assignment operator as per normal.The system will bind to the correct type on the fly. One Gotcha though, the keyword var needs to be initialised, thereby taking it’s type from the initialised value or object. If you don’t do this you will get a compile error: “Implicitly-typed local variables must be initialized”
Dynamic Type Example
Normal variable declaration:
int myvariable; or
string myvariable = “something”;
var variable declaration:
var myvariable = 1; or var myvariable = “something”;
Dynamic variable declaration:
We can also to the same for events and methods. Method or event can now have a dynamic return type as well as dynamic parameters. Take a look at the following code.
public int Calculator(int x, int y)
return x + y;
} public void TestDynamic()
var result = Calculator(1, 2);dynamic result2 = Calculator(3,4);
Methods or events that return a dynamic type:
public dynamic TestDynamic2(int x, int y)
return x + y;
Methods or events that have dynamic typed variables:
public dynamic TestDynamic2(dynamic x, dynamic y)
return x + y;
Are we losing it? Is C# now becoming a dynamic language? No it is not. It just means that you now have more options available. You still have to keep your wits about you. For example, in the above code passing two types that cannot be concatenated or added to TestDynamic2 will result in a runtime error.
Currently dynamic types can be used for generic type parameters, method parameters, method return types and so on. They however have a few limitations on them. For example – you cannot use the addition (+) operator on dynamic objects. In the long run however, they give us much easier access to unknown incoming object types. So instead of having to use reflection to crawl the type you could just use dynamic types instead.
Have you downloaded and tested the Visual Studio 2010 beta? What do you think? Leave a note in the comments below.
Testing Visual Studio 2010
Introduction to Web Apps with C#3.0 and Visual studio 2008.
New here, or perhaps you've been here a few times? Like this post? Why not subscribe to this blog and get the most up-to-date posts as soon as they are published.
0 comment(s) so far...
Do you want to receive blog updates via e-mail. Then just click on the link below. You will be redirected to Google's feed burner, where you can fill out a form. Supplying your e-mail address.
The subscription is managed entirely by Google's Feedburner. We cannot and do not collect your email address.
Subscribe to The Brave Programmer by Email
Sign up with Disqus to enjoy a surprise box of features