Some Last-Minute New C# 4.0 Features

Some Last-Minute New C# 4.0 Features

Rate This
  • Comments 51

As I’m sure you know by now, we are done implementing C# 4. We’ve added support for interoperability with dynamic languages and legacy object models, named and optional parameters, the ability to “link” against interfaces from a Primary Interop Assembly, and my favourite feature, covariance and contravariance of interface and delegate types.

Now, sometimes we manage to find time in the schedule to fit in small additional features that do not directly align with the larger “theme” of the release, but are useful features in their own right. In C# 3 we were all about LINQ features, but we did manage to also sneak in auto-properties and partial methods, neither of which directly supported our LINQ mission. But because they were clearly useful and relatively easy to design, implement, test and document, we got them in there.

Today I am announcing that we managed to get two additional operators into C# 4 that we have not hitherto announced: the “goes to” operator , written --> and the “is approached by” operator, written <--. In fact, we managed to get both of them into the last community preview, so if you have the CTP build, you can try them out today! (As you probably know, we do not like to ship features without running them by the community first.)

Here are examples of both operators:

int x = 10;
// this is read "while x goes to zero"
while (x --> 0)
    Console.WriteLine("x = {0}", x);

As you can see, this does exactly what you’d expect: x becomes 9, 8, 7, and so on, as it “goes to zero”. Our awesome compiler architect Neal heard via his contacts in the industry that the latest version of Java has this operator; wanting to maintain our massive lead in language features over Java, of course we worked overtime to deliver its benefits to our valued C# developer community.

Shortly thereafter, our awesome language specification manager Mads realized that we could generalize this to the inverse operator, the “is approached by" operator. We therefore implemented it too:

int x = 10;
// this is read "while zero is approached by x"
while (0 <-- x)
    Console.WriteLine("x = {0}", x);

And as you can see again, x takes on each value as it approaches zero. The exact semantics of this operator are subtly different than the goes-to operator, but I’ll describe the differences in a later post.

Pretty neat, eh? I am sure you guys can think of all sorts of wonderful uses for these new operators. The time pressure was really hard for this one, but they came through.

Finally, I know we've said that we want to maintain language-feature parity between C# and Visual Basic; unfortunately we were unable to get these operators into VB this time around. Perhaps in a future version.

UPDATE: Happy April Fool's Day everyone. For those of you who I fooled -- including at least one tester on the C# QA team, ha ha ha ! -- of course we are not announcing new operators the day before we ship C# 4.0.  "while (x --> 0)" is the same as "while( x--   >   0 )" which is of course perfectly legal and sensible code. This is an old joke; it works in any language with a "--" decrement operator.

  • HA HA HA HA HA. I crack myself up.

    This joke is not original to me of course; it's been making the rounds of programming web sites for a couple years now. If anyone knows the origin of it, I'd be happy to give credit where it's due.

  • Never seen that before.  If everyone I know didn't have Visual Studio's "auto-format" feature enabled, I'd have some fun showing it off.

  • I was about to ask if something like that would be syntactic sugar for a for loop

    while (x --> 0) would be for (int i = x; i >= 0; i--)

    Then, of course, I click on comments.

  • A whileGoesTo keyword would have probably been better, but its probably reserved by VB

  • 0 <-- x  performs one less iteration than x --> 0.

    Will this be fixed in C# 5 ? I already have critical production code which depends on this new feature.


    As I told you, there are subtle semantic differences between the two operators. That's one of them. :) -- Eric

  • @Mike, all me to present the "is run over by" operator, <=--

  • So can we expect the ==> operator in future versions of C# that incements by 2? This would be a very handy operator, too. tia ;-)

  • Eric,

    It was brought to my attention that the draft standard for C++11 also includes this new pair of operators. It must be an oft-requested feature!

    Anyway, I've noticed that the C++ version of the "is approached by" operator is more flexible in that it allows steps other than 1 with a very straightforward syntax modification; e.g.:

      while (0 <---- x); // step of 2

      while (0 <------ x); // step of 23

    Personally, I find it to be some brilliant language design, in excellent tradition of SHORT SHORT ... INTEGER and LONG LONG ... INTEGER of Algol-68. Are there plans to include this into C# for the sake of feature parity?

    In fact, I urge you to do that before Java does (they are also missing it, so far as I can see), to gain a competitive edge. It could just be the final nail in that coffin - the other significant one, of course, being that C# uses short and concise "bool" over Java's long-winded "boolean", resulting in significant increase of programmers' productivity.

  • I tried to make x start at 0 and "go to" 10 and got unexpected results. Compiler bug?

    I was able to work around by overloading a few of the operators for "int" to increment instead of decrement.

    I know that will break some existing code so I conditioned the custom operator on a global variable (wrapped in locks for thread safety) to enable the custom behavior.

    I think the new code is perfectly readable and really shows off the strengths and flexability of this new operator (also Java can't do it because it doesn't allow user defined operator overloading).

    static int operator--(ref int value)


       int result = value;

       if( g_fGoingUp )

           value = value+1;


           value = value--1;

       return result;


    static bool operator>(int value1, int value2)


       if( g_fGoingUp )

           return value1 < value2;


           return value1 > value2;




       g_fGoingUp = true;

       int x = 0;

       while( x --> 10 )

           Console.WriteLine("x = {0}", x);

       g_fGoingUp = false;


  • When do  you guys put in the while else loop?

    while (true)

       if (false)




  • hilarious.

  • I tried this with x declared as a float, and ended up with what seems like an infinite loop.  Am I doing something wrong?

  • @termserv:  For X as a float, you needed the approximation version of goes to/approached by:  while(0 <~~ X)  { }

  • Happy April Fool's day!

    Good one, had me going there for a few seconds!  God I hope that godawful feature, sugar for the prefix and postfix operators, is NEVER implemented.  But if it is at least leave it out of VB.  LOL

    It is implemented. Try it. (Hint: it is not a syntactic sugar for the prefix and postfix operators.) -- Eric

  • I would have preferred istrue and isfalse operators. You've no idea how much code I see that looks like:

    if (userLoggedIn == true) ...


    if (fileExists == false) ...

    Of course, like me, you wonder why these aren't double checked. If we had the aforementioned operators, we could write:

    if (istrue(userLoggedIn == true))


    if (istrue(fileExists == false))

    Much better. And no need for unit tests!

Page 1 of 4 (51 items) 1234