The Brave Programmer - Blogging and coding
Not for the faint hearted
 

Blog Posts From The Brave Programmer

Minimize

Dynamic Types in C# 4.0 – Visual Studio 2010

Aug12

Written by:
2009/08/12 10:01 AM RssIcon

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.

Tags:
Categories:
blog comments powered by Disqus
 
Blog Updates Via E-mail
 Blog Updates Via E-mail
Minimize

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

Print  
 

 

Latest Comments
 Latest Comments
Minimize
Powered by Disqus

Sign up with Disqus to enjoy a  surprise box of features

Print  
 
Blog Roll
 Blog Roll
Minimize
Print  
 
Categories
 Categories
Minimize
Print  
 
<h1>Search Blogs From The Brave Programmer</h1>
 

Search Blogs From The Brave Programmer

Minimize
Print  
 
Archive
 Archive
Minimize
Archive
<March 2017>
SunMonTueWedThuFriSat
2627281234
567891011
12131415161718
19202122232425
2627282930311
2345678
Monthly
Go
Print  
 
<h1>News Feeds (RSS)</h1>
 

News Feeds (RSS)

Minimize
Print  
 

Follow robertbravery on Twitter

Blog Engage Blog Forum and Blogging Community, Free Blog Submissions and Blog Traffic, Blog Directory, Article Submissions, Blog Traffic

View Robert Bravery's profile on LinkedIn

Mybyte

 

Robert - Find me on Bloggers.com

Tags
 Tags
Minimize
Print  
 
Contact Us Now
 Contact Us Now
Minimize
 

Email  us now or call us on 082-413-1420,  to host your website.

We design and develop websites. We develop websites that make a difference. We do Dotnetnuke Module development.

Web Masters Around The World
Power By Ringsurf
Print