Dynamic Types in C# 4.0 – Visual Studio 2010
Aug12Written 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.
The DLR is built on top of the Common Language Runtime (CLR) and opens the door for a common set of technologies and tools to interoperate inside a common framework. Dynamically typed languages such as Python, Ruby, and JavaScript can reside alongside familiar statically typed .NET languages such as C#.
Statically typed languages such as C++, C#, or Java perform type checking at compile time. In contrast, dynamically typed languages such as JavaScript, PERL, and Ruby perform type checks at runtime.
The var and dynamic keywords.
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:
dynamic myvariable;
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;
}
Is C# a dynamic language?
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.
Related Reading:
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.
blog comments powered by