how evaluation order in C# can cause unexpected surprises for multi-threaded (or single threaded!) applications

Microsoft in general has the best overall tech blogs of any major tech company (IMO), but the one that really shines of the bunch is The Old New Thing by Raymond Chen. It's very technical and a great source to learn from, but it's also filled with interesting annecdotes and good humor.

The most recent post details some unexpected consequences of the C# parser and execution order. For instance, the += operator causes some interesting behaviors if you aren't very familiar with the language spec:

class Program
    static int x = 0;

    static int f()
        x = x + 10;
        return 1;

    public static void Main()
       x += f();
       System.Console.WriteLine(x); // Prints 1!

Leave a comment -


I'm a .NET, PHP, Python, and Javascript developer. Also Project Manager, Scrum Master, and Juilliard trained violist. You cannot connect with me on Facebook, Twitter, or LinkedIn so don't ask.