Skip to content

Will this compile?

Akkiraju Ivaturi

Akkiraju Ivaturi

Asked Sep 7, 2012, 7:24 PM EDT1 answer
Share
LinkedInX
C#
using System;

class Test
{
    enum Foo { Bar, Baz };

    const int One = 1;
    const int Une = 1;

    static void Main()
    {
        Foo f = One-Une;
        Console.WriteLine(f);
    }
}

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Akkiraju Ivaturi
    0
    This won't compile under the MS C# 2 compiler, but will compile with the MS C# 3 compiler. It's a known bug due to some optimisation being done too early, collecting constants of 0 and thinking that any known 0 constant should be convertible to the 0 value of any enum. It's with us now, and unlikely to ever be fixed as it could break some code which is technically illegal but working perfectly well. It's possible that the spec will change instead, of course. 
    0