Monday, October 27, 2008

The Excitement that is C# 4.0



kick it on DotNetKicks.com
I just got back from the “Future of C#” talk at PDC by Anders Hejlsberg. This was a truly inspiring talk for a geek like me. C# is evolving into a much more dynamic language. I have always been a believer of strong typing… except when I’m not… and I have been wishing for something more dynamic (such as Duck Typing). In C# 4.0, we will be seeing some significant dynamic features.

In reality, the thing that has kept me away from using languages such as IronPython and IronRuby is their interoperability with strongly typed languages. I really believe in the concept of “The right language for the job”, but I hate the idea of sticking to that one language for the entire project. With the dynamic capabilities in C#, it will be MUCH easier to talk to Python or Ruby code. If I need to implement something really loosely (like a calculation engine), I will be able to jump into something loose. Then, when I want to work with that code in my more strongly typed environment, I will have that ability. The “Right Language for the Job” paradigm has just become much finer grained.

So here are the details.

Dynamic Keyword
First and foremost is the dynamic keyword. This is kind of like using the object keyword, but you are saying that all of your binding will be at runtime. You will loose your intelisense, of course, but you will now be able to call into methods that have not been previously defined.

The neat thing about this is that you can make your statically defined classes be dynamic by implementing the IDynamicObject interface, which allows you to have access to the late binding calls.

Named and Optional Parameters
Next is something that C++ has and C# has needed for a long time – Optional Parameters. You can set defaults in your method declaration and the caller doesn’t need to specify the parameters. In addition, you can name the parameters in the method calls. This is really great for readability… especially when you are passing a bool into a method that you have no idea what it does.

Better COM Interoperability
These previous features (Optional and Named Parameters) are really useful to add to the new COM Interoperability features. Basically, pairing the dynamic and parameter features, talking to COM controls looks very natural.

Covariants and Contravariants
Finally, but certainly not least, we are getting covariance and contravariance. This is something that has bugged me since I have started with C#. Currently, if a method takes IEnumerable<BaseType>, you can’t pass IEnumerable<DerivedType>. I Hate having to convert the derived set to a base set just to pass it in. In C# 4.0, this will be fixed.

Looking Forward
In future version of C#, we will start to see hooks into the compiler. This will open the door for Domain Specific Languages (DSL) and Meta Programming. We will also see the C# compiler as an embedded service within .NET. This means that we can run Eval(“doSomething()”). It also means that we can start to think about embedding C# as a scripting language for our application… much like Javascript in the HTML DOM.

I am hoping to see C# 4.0 soon. Better yet, I am hoping to see C# 4.0 in the bits we get tomorrow!

2 comments:

senfo said...

I am personally not a fan of duck typing. In fact, I find it to be the case that it's almost always used to make up for a bad design. The Criticism section of the Wikipedia page gives a brief understanding as to why I feel this way.

Brian said...

I don't love duck typing, myself. But, I do find it to be a very useful tool in some situations. Sometimes, it is the best tool for the job.

My philosophy is: Give me the knife. If I cut myself, that is my problem. Don't withhold the knife just because you think I can't use it responsibly... Kind of like multiple inheritance.