C# 4.0

31 October 2008 · 5 minute read · c#

As most people would know PDC is on at the moment over in the US and as usual Microsoft is showing their bag-o-tricks about what they are working on.

With PDC we saw a CTP release of Visual Studio 2010, and with this brings the .NET 4.0 framework and the next incantation of the C# language, C# 4.0.

I recently watched a screen cast session from PDC on the future of the C# language (link here), a session run by Anders Hejlsberg who is an excellent authority in the area of programming language design.
I strongly recommend that you watch the session if you are interested in where C# is going as a language. Be aware it's a 70 minute session and pretty full-on in some parts.

So what are the new features coming with C# 4.0?

The Dynamic keyword

Anyone who's done a lot of work with C# 3.0, particularly with hard-core Lambda shouldn't be surprised by this move. C# is getting more dynamic programming features built into it, through the use of a dynamic keyword. Justin Etheredge has done two good posts which look at the dynamic keywork and how it can be used. Anders also has a good demo in the screencast.

For me it's a little too early to have much of an opinion on this feature, I'm definately in two minds over it. On one side I really like the ability C# is going to have to tie straight into Ruby or Python or JavaScript with next to no changes to the code, but on the flip side is brings in a greater chance of errors.

ASP.NET developers are familiar with dynamic languages in the form of JavaScript, and any ASP.NET developer who's done a lot of JavaScript will tell you just how much of a pain in the arse it can be to debug. Because there's no compiler we don't know until run-time that there's a problem. Additionally intellisense suffers in a dynamic world vs a static one.
And that's something I noticed from Anders talk, that when we're using the dynamic keyword in C# we loose the intellisense capabilities.

Until I have a chance to actually play with it in practical scenarios I'm not going to know whether it's really a useful idea at the moment.

Named and default parameters

What can be said about this other than "about fucking time!". Essentially this means that when defining method stubs parameters can be given a default value so they are optional in use. No more writing stacks of overloads to cater for every scenario of missing parameters, now it's just a single method with the defaults flagged appropriately.

Named parameters are also really nice, and to me it feels a lot like JSON parametering on methods in JavaScript. Just define the parameters to pass in and problem solved.

Both of these are to a certain extent syntactic sugar. It'll be interesting to see just what the compiler generates at an IL level to see what kind of performance hit may be resulted from this.

Co and Contra variants

This is definitely an interesting concept and something that I'm still getting my head around. For that reason I wont go into it here but watch the screen cast for more information.

From what I'm understanding of it they are going to be useful and it'll bring another level of power on top of the already awesome Generics framework.

Improved COM interop

Ok, so not really a commonly used feature of ASP.NET but the improved COM support (some-what as a side effect of default parameters) does mean that when coding against COM, like the Office interop, will be a whole lot nicer and a whole lot more viable.

C# post-4.0

So the last bit of the screen cast is Anders talking about what they are looking at with C# post version 4 and what he talks about is the compiler and it's ultimate role in the language.

Anyone who reads Bart de Smet's blog will have seen he did a Channel9 video recently (and if you didn't watch it you should!) where he talked about something Anders demos. It's the concept of the C# compiler as a service. This means that you can dynamically generate C# code which is then passed to the compiler and executed on the fly.

If you're not really sure what this means, check out the program LinqPad (I blogged on it last month), LINQPad opens up the compiler in a similar way, making it something that you can write code and pass to, rather than having to write code, make a DLL and execute.

Compiler as a Service is a very cool concept and really opens up the ability to generate code on the fly. Less reliance on CodeDom and System.Reflection is a great idea (as I can attest to from my work on the UIL!).


The C# future definitely looks like a bright one. C# 4.0 doesn't seem to quite be the 'knock your socks off' release that C# 3.0 was but it's a move in the right direction and a very interesting one for sure.

Published: 2017-10-30 16:57:41 +1100 +1100, Version: 9ff0cbb