This post is about the session called C# 4.0 / The Future of C# given by Krishnan Subramanian.

First off let me start with a quick summary of main new feauture for the different versions of the C# that are released.
- C# 1.0, Managed code
- C# 2.0, Generics
- C# 3.0, Language Integrated Query, short LINQ
- C# 4.0, Dynamic programming
Some off the innovation for C# 4.0 are:
Dynamic Language Runtime
The new version of C# has a new type of object declaration, called dynamic. This looks a bit the var keyword. With the var keyword the compiler replaced the var with the object type on compile time, intellisense and code completion still was available in Visual Studio. With the new dynamic keyword the compiler doesn’t replace this and even more important the compiler can’t check for syntax error on a dynamic object. Intellisense and code complition are also not available for objects defined with dynamic.
Dynamic objects are losely typed instead of strongly.
After reading this you might think whats the use of this new DLR, in the following sample I’ll try to explain this. We have a simple C# class called Calculator, and use it like below.
Calculator calc = GetCalculator();
int sum = calc.Add(10, 20);
Now suppose the calculator class is not strongly typed, the code would like something like the following
object calc = GetCalculator();
Type calcType = calc.GetType();
object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, new object[] { 10, 20 });
int sum = Convert.ToInt32(res);
With the new dynamic keyword in C# 4.0 it would simply be
dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);
Optional Named Parameters
To show you the what this means take a look at the examples below.
In previous version of C# method overloads would be used.
public void Add(string lineOfText);
public void Add(string lineOfText, bool isError);
public void Add(string lineOfText, int repeat);
public void Add(string lineOfText, int repeat, bool isError);
With the new C# 4.0, the method declaration would like this
public void Add(string lineOfText, int repeat = 1, bool isError = false);
As you can see in the sample above, it’s now possible to specify default values for a method parameter. Some samples of how to use the Add method are:
Add("This is the line", repeat : 5);
Add(isError : true, lineOfText : "Another line of text");
For people using JavaScript this might look very familiar.
Another cewl new thing I saw at the demo, was the ability to interpret and compile a string of C# code at runtime. This functionality offers some great new possibilities. Again for the Javascript people reading this it’s like the eval function.