Should this code compile? Does it? What does it mean?
Answer: This shouldn't compile, but it does under the MS compilers for both C# 2 and 3 (and probably 1 as well - I haven't checked). It shouldn't compile because only the literal 0 should be implicitly convertible to the default value of any enum. Here the decimal is 0.0. Just a little compiler bug. The result is to print
using System;
class Test
{
enum Foo { Bar, Baz };
static void Main()
{
Foo f = 0.0;
Console.WriteLine(f);
}
}
class Test
{
enum Foo { Bar, Baz };
static void Main()
{
Foo f = 0.0;
Console.WriteLine(f);
}
}
Answer: This shouldn't compile, but it does under the MS compilers for both C# 2 and 3 (and probably 1 as well - I haven't checked). It shouldn't compile because only the literal 0 should be implicitly convertible to the default value of any enum. Here the decimal is 0.0. Just a little compiler bug. The result is to print
Bar
as that's the 0 value of the Foo
.
Comments
Post a Comment