简体   繁体   中英

Enum bit operations with Mono

I'm trying to port program from .net to Mono on Mac OS X, but I can't compile it while I'm getting compilation error. What Should I do to compile it ? Why on windows it works fine?

I'm new to C#, especially on mac os.

namespace my-project
{
    using System;

    [Flags]
    public enum MyEnum : uint
    {
        F_FLAG_1 = 0x00004,
        F_FLAG_2 = 0x00008
    }
 }

Code line with error:

if ((this.MyEnumField & num) != 0)

Compilation Error message:

Error CS0019: Operator `&' cannot be applied to operands of type `MyEnum' and `uint' (CS0019) (my-project)

I'm using Xamarin studio.

I can't read it as anything else than that Mono is in the right, the C# spec has this to say;

Enum values and operations

Each enum type defines a distinct type; an explicit enumeration conversion (§6.2.2) is required to convert between an enum type and an integral type , or between two enum types

...and...

Enumeration logical operators

Every enumeration type E implicitly provides the following predefined logical operators:

  • E operator &(E x, E y);
  • E operator |(E x, E y);
  • E operator ^(E x, E y);

That is, the logical operators are only defined enum*enum, and to use an integral type in a logical expression with an enum, it should require an explicit cast.

如果它使用.NET而不是Mono进行编译,则是Mono编译器错误,请在http://bugzilla.xamarin.com/中进行报告。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM