When I began to write ZweiDe back in 2007, the target framework was .Net 2.0. The third framework version was already released, but I assumed it would severely impact compatibility to require the newest stuff available. As it took years to bring ZweiDe anywhere near release, it was of course irrelevant. I didn’t care much since I had no practical idea of what I was missing anyway. In Duality, calculating with an estimated development time of forever, I figured out it might be worth a try targeting .Net 4.0 right from start.

I read some wiki entries to get a quick glance at the new language features and classes. Then some tutorials. It all seemed nice, yet not quite necessary – but after a while, I could hardly go back without really really missing something. If I had to pick a single, favorite feature, I would chose Extension Methods. As their name suggests, they allow you to extend existing classes with your own methods. Simple case: Assume you need a random color. Here’s what you’d probably do without Extension Methods:

Using Extension Methods, the code looks like this:

Pretty much some kind of syntactical sugar. But one that feels extremely elegant; instead of accessing some seemingly unrelated class, you write your class instances variable name, hit the dot button, the auto completion menu pops up and there they are: All those helper methods extending that specific class.

However, let’s take it a little further: Extension Methods can also be defined generic and extending interfaces. C# 4.0 comes with IEnumerable extensions providing a lot of useful set operations (which by the way would be my second favorite feature). In context of Dualitys component-based GameObject system, I added some custom ones:

Those Extension Methods simplify things a lot. Whenever there is an enumeration of Components, it’s just a single word to query all of their GameObjects.

Here’s some actual usage example from the Duality Editor:

Yeah, I know. It’s not impressive. But so damn handy.