Scott Dorman

ephemeral segment

  Home  |   Contact  |   Syndication    |   Login
  566 Posts | 9 Stories | 571 Comments | 51 Trackbacks

News


Post Categories

Image Galleries



Creative Commons License


Microsoft MVP


MCP Profile


Locations of visitors to this page

Subscribers to this feed

TwitterCounter for @sdorman

View blog authority

Add to Technorati Favorites

Windows Live Alerts

AddThis Social Bookmark Button

LinkedIn profile

Community Credit profile

The Code Project

Follow me on Twitter

Get Free Shots from Snap.com

Community Credit Hall of Fame

Get Feedghost

Xobni outlook add-in for your inbox

Windows Live Translator


Support This Site

Tag Cloud


Article Categories

Archives

Post Categories

Image Galleries

If C# 3.0 was all about Language Integrated Query (LINQ), then C# 4.0 is all about dynamic programming. What exactly does that mean? It means that C# 4.0 brings some of flexibility and declarative style of programming to C#.

image

But what does that really mean?

To sum it up in one keyword: dynamic.

C# 4.0 is adding a new dynamic keyword which is used as a data type in much the same way the var keyword is used. Why is this important? The biggest reason is that it allows a C# program to use dynamic dispatch to more naturally create objects coming from a dynamic language.

For example, suppose you have a Calculator object declared in C#, meaning it is statically typed. You interact with your object like this:

Calculator calc = GetCalculator();
int sum = calc.Add(10, 20);

That’s pretty simple and straight forward. Now suppose the Calculator is not a statically typed .NET class (or it is a .NET class but you don’t know the specific type of class), you must do something like this:

object calc = GetCalculator();
Type calcType = calc.GetType();
object res = calcType.InvokeMember("Add",
    BindingFlags.InvokeMethod, null,
    new object[] { 10, 20 });
int sum = Convert.ToInt32(res);

That’s not nearly as simple. In fact, it’s downright ugly. There is a lot of non-type-safe calls and reflection going on here that you really shouldn’t have to see.

To take this a step further, if we knew that Calculator was a JavaScript class, you must use similar (but still significantly different) code:

ScriptObject calc = GetCalculator();
object res = calc.Invoke("Add", 10, 20);
int sum = Convert.ToInt32(res);

The reason for the differences in syntax is that there is no unification between the two APIs.

In C# 4.0, you can now use the following syntax:

dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);

If you look at this syntax and the earlier statically typed call, you should notice that the only difference is that in C# we are declaring the data type to be dynamic.

image

Does this mean that C# is loosing it's roots as a statically typed language or that we should all start moving towards dynamic languages? Absolutely not. What is means is that it is now easier for you to write C# code that talks to objects (or APIs) written in dynamically typed languages. It also means that there is a unified API to talk to any dynamic language. You no longer need to worry about what language you are interoperating with to determine which C# code you must write.

So how does the dynamic keyword work? As I mentioned, it's a keyword in a similar fashion to var. You declare at compile-time the type to be dynamic, but at run-time you get a strongly typed object.

image

The dynamic keyword is great for writing C# code that consumes a dynamic object, but what about going the other direction and writing C# code that can be called from a dynamic language? You do this by implementing the IDynamicObject interface (or more simply, inheriting from the abstract DynamicObject class) and providing your own implementation for the member lookup and invocation.

Using the features and capabilities of the new dynamic keyword, the IDynamicObject interface, and the fact that the dynamic dispatch can dispatch to both dynamic and static types, C# effectively gets support for duck-typing.

Technorati Tags:
DotNetKicks Image
posted on Sunday, November 16, 2008 10:11 AM

Feedback

# re: C# 4.0: Dynamic Programming 11/16/2008 12:50 PM Catto
Hey Now Scott,
that is pretty good info.

Thx,
Catto

# re: C# 4.0: Dynamic Programming 1/19/2009 4:17 PM Dan
Hi Scott,

Can 'dynamic' type for Generic?
For example, List<dynamic>??

Thanks,

Dan

# re: C# 4.0: Dynamic Programming 3/13/2009 5:09 PM Janus007
Well, I can see a some nice scenarios to use this.

But I had hoped for something much more powerful

private string Foo(int x)
{
return x + 10;
}

public DoSomeThing(Method name)
{
var fooValue = name(10);
}

public Caller()
{
DoSomeThing("Foo");
}

That's what I call dynamic OR even more useful, dynamics for class names also.


Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: