What I’d like to see in C# 5

Lately my work has made me quite the polyglot. Although I’m still in C# most of the time, I’m also writing bits of Groovy, Ruby, Java and of course JavaScript because I’m that cool. There are many things to like about C# – LINQ, lambdas, extension methods, anonymous types, etc… All of these things make C# a wonderfully productive language for the applications that I write. There are however a few features I get to use in other languages which I feel could make wonderful additions.

String Interpolation

A simple one to start with. Python, Ruby and Groovy let you use code expressions inside strings which are immediately evaluated.

var dave = new Person { Name = "David" };
var welcomeText = "Hello ${dave.Name}!";

So you’re probably thinking that this isn’t all that different from just using string.Format(“Hello {0}!”, person.Name) and fine, there’s really not much difference. But I’d argue that having these kind of things built into the language really help with general productivity. Miguel de Icaza of Mono fame also happens to agree with me and has even proposed a workable syntax to support it.

Range Indexing

We’re all familiar with indexing arrays – you give it a number and it returns the item at that position. Other languages take this much, much further providing powerful indexing abilities.

var a = { 1, 2, 3, 4, 5 };
var secondItem = a[2];
var lastItem = a[-1];
var middleItems = a[1..3];
var everyOtherItema = a[0, 2, 4];

Again these things are nothing that can’t already be achieved by using existing functionality – LINQ provides a lot of this. However these are operations we do so often – why not add language support for them?

Regular Expressions

I’m a big fan of regular expressions. If they’re not one of the tools in your belt then you’re missing out big time. The FCL already provides a fairly quick way of testing strings against a regular expression.

RegEx.IsMatch("^/d+$", "123")

Ruby among others goes a step further and bakes this right in the language. I’d like to see C# make regular expressions a primitive type like a string or an integer. Then maybe offer operators to make testing values against them more succinct.

var regEx = /^[0-9]+$/;
var isNumber = "23" =~ regEx;

Actually now I look at the operator I’m not entirely sure if I like it, but it could be a nice idea.

Grander Things

There are some bits of Ruby that make me very jealous. I keep thinking to myself that Mixin’s and some of the incredible meta-programming techniques could really help writing reducing some of the pain I find writing ViewModels for my WPF and SL applications. How would this integrate into C# I don’t know. Of course with IronRuby officially out the door perhaps just sticking with Ruby is an option. Expect to see more thoughts from me in that direction in the future.


I feel that the above would make C# slightly more productive, but would it be worth the extra complexity? Perhaps not. I’m pretty sure none of us want to end up with something like Perl.

More importantly, from the rumors coming out of Redmond it sounds like the big focus of C# 5 will be exposing the “compiler as a service”. I’d rather have something like the awesome pluggable/expendable pipeline that powers Boo in C# over these rather minor enhancements.


7 thoughts on “What I’d like to see in C# 5

  1. let x = [0..5] totally works in F#, just not in OCaml. The F# team figured since they were making a new language they were entitled to shamelessly steal from other languages like Haskell.

    I was mistaken about the regular expressions. I could have sworn match did some form of string pattern matching, but alas, it does not.

    I’m not entirely sure whether Perl-style strings would necessarily be a good thing for the same reason I don’t like string.Format as opposed to printf in OCaml/F# – there’s no type checking going in there, so unexpected results are possible. Consider if (for some reason) another developer came in and replaced your string Name property with a LegalName Name property, where LegalName was a more complex type capable of understanding all the strange variations of human names. All of a sudden your string printer would start printing a class name (assuming they didn’t override ToString) and you’d get a transparent regression. By contrast, printf(‘Hello, %s’, dave.Name) wouldn’t even compile, alerting the dev changing the Name property type to update your string printer, too.

  2. I’m probably a head in the sand C#er (also did JS, PHP, VB, SQL, a little C++ and JAVA, 2 flavors of assembly, etc., but I’m all about C# right now), but I think LINQ and extension methods cover this pretty well.

    Core features should remain core, less is more.

    Maybe you’re talking about an extension library here.

    Loved your Meetup demo btw.


  3. I can’t stand String.Format(“…”, …) as well.

    public static string FillIn(this string format, params object[] obj) { return String.Format(format, obj); }
    reads much much better. Almost good enough for me.

    When it comes to strings I miss Perl’s q{}/qq{} and heredocs much more.
    var html = String.Format(“{1}“, address, “Click here”, “external_link”);
    var html = qq{{1}}/FillIn( address, “Click here”, “external_link”);
    No need to escape or duplicate doublequotes or anything else in strings if you choose the right delimiter.

    1. Oops. Yet another comment syntax and no preview …

      Let’s see if I can convince WordPress to leave the code alone:

      When it comes to strings I miss Perl’s q{}/qq{} and heredocs much more.
      var html = String.Format("{1}", address, “Click here”, “external_link”);
      var html = qq{<a href="{1}" target="_blank" class="{2}">{1}</a>}.FillIn( address, “Click here”, “external_link”);

      1. No, it will not. Hopefully you get the point. Within doublequoted strings you have to escape the doublequotes, the ability to choose the delimiter would allow you to paste things like snippets of HTML into string literals without wasting time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s