C# 4.0

I tested it in VS2010. Which is beautiful by the way, I like the WPF rendering. And I’m happy they didn’t disable the possibility to generate .Net 2.0 assemblies. I wanted to test the new historical debugger functionnality, which enable you to see the state of variable in the past time, but I didn’t take the Team System Edition (and I’m too lazy to download it again).

named and optionnal arguments
I’m so happy Microsoft has created optional and named arguments for method calls. It avoids a lot of stupid method overload. And I just can’t way to be using it in my code.

This is also a good news for people using Interop methods, like the one for Word 2007. I’ve seen a code using them, it looks like that :

1
2
3
object nullObj = null;
object name = "doc.docx";
officeClass.SaveAsWord2007Document( ref name, ref nullObj, ref nullObj, ref nullObj, ref nullObj, ref nullObj, ref nullObj, ref nullObj, ref nullObj, ref nullObj, ref nullObj, ref nullObj, ref nullObj );

With C# 4.0, it could be translated to something like that :

1
officeClass.SaveAsWord2007Document( "doc.docx" );

The C# 4.0 will convert it to exactly the same IL code internally (creating a refNullObj).

But it’s still just a little COM+ specific feature, you can’t use in Mono/Linux for instance. So I don’t think we can consider it as a real language feature.

DLR
I tested DLR with the “dynamic” type, I think it should only be used when we have no other choice (when we want to communicate with some scripting languages for instance). One reason is that it breaks the VS auto-completion, it’s like you were typing your code in notepad. The other reason is that, from my test, dynamic calls are 3 times slower to execute than compiled calls :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
static void Main() {
	int nb = 100000000;
 
	Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
 
	TimeSpan without, with;
 
	{ // Without dynamics
		Console.WriteLine( "Without dynamics..." );
 
		var c = new Clacla();
 
		var start = DateTime.Now;
		for ( int i = 0; i < nb; ++i ) {
			c.DoSomething();
		}
		without = DateTime.Now - start;
		Console.WriteLine( "Elapsed time : {0}", without );
	}
 
	{ // With dynamics
		Console.WriteLine( "With dynamics..." );
 
		dynamic d = new Clacla();
 
		var start = DateTime.Now;
		for ( int i = 0; i < nb; ++i ) {
			d.DoSomething();
		}
 
		with = DateTime.Now - start;
		Console.WriteLine( "Elapsed time : {0}", with );
	}
 
	Console.WriteLine( "Dynamics call are {0} times slower.", Math.Round( (double) with.Ticks / without.Ticks, 2 ) );
 
	Console.ReadLine();
}

The Clacla class is a totally useless class :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Clacla {
	private string _s;
	private int _x, _y;
	private dynamic _d;
 
	public Clacla( int x = 1, int y = 2, string s = "", dynamic d = null ) {
		_x = x;
		_y = y;
		_s = s;
		_d = d;
	}
 
	public int DoSomething() {
		return (int) Math.Pow( X, 2 ) + Y;
	}
 
	public int X { get { return _x; } }
 
	public int Y { get { return _y; } }
 
	public string S { get { return _s; } }
 
	public dynamic D { get { return _d; } }
 
	public override string ToString() {
		return String.Format( "X={0}, Y={1}, S={2}, D={3}", _x, _y, _s, _d );
	}
}

The result is :

1
2
3
4
5
Without dynamics...
Elapsed time : 00:00:14.7285156
With dynamics...
Elapsed time : 00:00:39.8613281
Dynamics call are 2,71 times slower.

Covariance / Contravariance

It will simplify things for sure but I’m a little disappointed by the remaining restrictions.

The best explanation I could find on it so far is here.

C# + IL
I think Microsoft really had a great idea when they created their “Intermediate Language” to split the language from the managed library. Because if Sun had done the same thing you would be able to use enums (which is just an “int” internally) in an “old” 1.4 JRE.
With C# 4.0, you can still code for “old” .Net 2.0 environnements.

Leave a Reply

Your email address will not be published. Required fields are marked *