Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is field referencing not allowed in an enum (or is this a compiler bug?)

Tags:

c#

enums

mono

When I use the following code:

using System;

namespace Foo {

    [Flags]
    public enum Bar : ulong {
        None          = 0x0000000000000000,
        A             = 0x8000000000000000,
        B             = 0x0000000000000001L | A,
        C             = 0x0000000000000002L | B,
        D             = 0x0000000000000004L | A,
        All           = A | B | C | D
    }
}

The mono compiler crashes. Is this due to a semantical error (something that is not allowed in the language), but is unnoticed by the compiler or is this a compiler-bug?

Version: Mono 2.10.8.1

I've filed a bug report at bugzilla (https://bugzilla.xamarin.com/show_bug.cgi?id=15801)

like image 576
Willem Van Onsem Avatar asked Oct 29 '13 21:10

Willem Van Onsem


1 Answers

From the C# language spec, section 14.3:

The associated value of an enum member may not, directly or indirectly, use the value of its own associated enum member. Other than this circularity restriction, enum member initializers may freely refer to other enum member initializers, regardless of their textual position.

So your code should be correct (as it has already been suggested in the comments), and thus it should be a compiler bug.

like image 60
Matthias Avatar answered Nov 17 '22 16:11

Matthias