TechEd session DEV340: Visual C# under the covers (Anders Hejlsberg, MS)

published: Fri, 10-Jun-2005   |   updated: Tue, 21-Jun-2005

Unfortunately, for me this session was summed up by one word: disappointing.

Essentially the talk was the same one Anders has been giving over the past two/three years at various conferences (for example the PDC in 2003). In fact, it's also the one I gave at BorCon 2003: I'd borrowed Anders' slidedeck for the session.

That's not to say that it was a pleasure hearing Anders talk. He has a facility with words, and bottomless knowledge about his subject. Unfortunately the only real reason I went is because of this sentence at the bottom of the session's blurb: "Finally, Anders discusses possible future directions for the C# language." Uh, no, he didn't, not in this session anyway. He vaguely said something about having the compiler "infer" types so that you don't have to declare them (this is already visible in C# 2.0 when instantiating a delegate -- you don't have to specify the type). He also intimated something about marrying up C# to the database world more. Anyway, he's giving a talk at PDC this year and will have a lot more to say about possible directions for C# 3.0. Must try and be there.

So we were treated to the usual talk about generics (with the familiar List and List<T> example), anonymous delegates, iterators, nullable types (see below), and partial classes. He also covered quickly static classes, property accessor accessibility (i.e., making the set accessor private or internal for a public property), fixed size buffers for unsafe code, and the #pragma warning statement.

Anyway, there were a couple of changes as far as I remember. The ability to specify class or struct (constraining to reference type or value type) as a constraint for generic types was something that was being batted around when I was at MS, and it's made it into the language.

There was also a bit about the new language syntax for nullable types. Although this is familiar to me, I can't remember whether this was around back then or not, I think not: it's been added in the meantime. Essentially it's a shortcut: you can easily declare nullable value types like this:

int? x = 0;     // the nullable int x has the value 0
int? y = null;  // the nullable int y has no value

There is also a new coalescing operator, ??, which enables you to rewrite something lengthy like this:

int? x;
..code which sets x somehow..
int y = x.HasValue ? x.Value : -1

as

int y = x ?? -1;

In other words the coalescing operator enables you to "remove the nullness" of a nullable type by returning a default value. It even works with reference types (which can obviously be null in and of themselves). For instance, a converting a null string to String.Empty becomes:

string s;
..set s perhaps..
string t = s ?? String.Empty;