A blog by Gary Bernhardt, Creator & Destroyer of Software

C# 3.0 Looks Promising

24 Jan 2007

At CodeMash last week, I learned quite a bit about C#'s new features from Scott Guthrie. I'm no fan of explicitly typed languages , but C# 3.0 has me very excited. It's getting a host of new features inspired by other languages, including anonymous types, type inference, and lambda expressions. There's plenty of discussion of these around the web, so I'm not going to rehash them all. I want to highlight the two features that I'm most excited about: type inference and LINQ.

Type Inference

C# 3.0 adds basic type inference, but it's quite weak and can only infer types on assignment. Basically, you can do these types of things:

var i = 5;
var s = some_function_returning_a_string();
foreach (var s in list_of_strings) { ... }

Note that the "var" keyword does not make the variables dynamically typed – it just tells the compiler to infer their types based on what's being assigned to them. This is nice, but it's very limited. You can't, for example, declare a function with a return type of "var" and expect the compiler to figure it out. Haskell this definitely is not, but it's a huge step forward for C's verbosity-laden children.

LINQ

Let me get this out of the way: LINQ is awesome and I want it in my language.

In a nutshell, LINQ allows you to use declarative, SQLesque syntax in your C# code. I've been wishing for this feature for a long time, but Python's list comprehensions have had to hold me over so far. Here's a very simple example of what you can do with LINQ:

int[] numbers = {5, 4, 1, 9, 8};
var low_nums = from n in numbers
               where n < 5
               select n;

Here's an equivalent example in Python:

numbers = [5, 4, 1, 9, 8]
low_nums = [n for n in numbers if n < 5]

LINQ is far more powerful than Python's list comprehensions, though. It has many of the features of SQL: joins, grouping, count() and sum(), etc. And a single LINQ expression can query a collection of objects, a database, or XML data. This makes me drool.

Most of the discussion of LINQ at CodeMash boiled down to excitement over the idea of writing statically-typed, compiler-checked database queries. You can probably guess that this is not the reason I'm excited. For most interesting problems, the limiting factor is not whether you get your queries correct or not; it's whether you can coax your brain into (1) solving the problem and (2) translating the solution into code. Any feature that simplifies your code makes step (2) easier, and LINQ is definitely such a feature. LINQ is the only feature of any explicitly-typed language that I am covetous of. It really is that awesome, and Microsoft has managed to beat every other language to the punch with this.

Back to Reality

OK, enough gushing. I still dislike explicitly typed languages. These new features certainly aren't going to get me to leave dynamic languages behind – C# is still incredibly verbose when compared to Python, Ruby, etc. And, even though many of these features were borrowed from functional and dynamic languages, C# 3.0 is still as statically typed as ever. However, I have a newfound respect for Anders Hejlsberg and friends. If Microsoft manages to drag the hoards of C# programmers toward a more concise coding style, the world will be a much better place.