Tuesday, March 3, 2009

C# 3.0 features that clean up (a bit) the code

I work to a new feature (so stay tuned!) but I wanted to describe some features that are used in NaroCad and are specific to C# 3.0 (part of .NET 3.5 and Visual Studio 2008).

We don't use LinQ and lambda expressions for now but some features may interest other developers when they have to handle our code:1. The var keywordOld codeSortedDictionary<int,Node> _childNodes = new SortedDictionary<int,Node>();New code:var childNodes = new SortedDictionary<int,Node>(); (the compiler will decide the type, and the code is equivalent)var removes a long left side declaration and it is shorter than most types you can define. The issue is that using var keyword extensively you will not be able to decide which type you run.var x = f(); //where f returns a type intx = 3.1415; // it will give to you a compile error as you want to use it as a float type//but is declared as int