I stumbled upon this weird thing today:
http://www.yoda.arachsys.com/csharp/teasers.html
Question #5.
The code:
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);
}
}
Now according to the answers on http://www.yoda.arachsys.com/csharp/teasers-answers.html for question #5
... 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.
But why?
One & Une are both const. I.e. they can be calculated compile-time, so it becomes
Foo f = 0. And since 0 is a valid value for any enum, why shouldn't this compile?