5

I've got an enum like this in an old piece of code:

[Flags]
public enum Example: uint
{    
    Foo = 0x00000001,
    Bar = 0xC0000000
}

Now, FxCop is complaining about this enum using uint rather than int as it's backing field. (And I've been tasked with getting this code as FxCop clean as possible...) But there is an existing enum value which uses the high order bit of the enum, and I can't change this because it has been persisted into an on-disk format. When I try to compile this, the C# compiler rightly complains:

error CS0266: Cannot implicitly convert type 'uint' to 'int'. An explicit conversion exists (are you missing a cast?)

So, I was going to change it to this instead:

[Flags]
public enum Example
{    
    Foo = 0x00000001,
    Bar = (int)0xC0000000
}

However, I'm not positive that I can rely on this not throwing arithmetic exceptions, or not being handled correctly when being read or written to disk. Can I rely on the bit format here matching the format it used when the enum was backed by an unsigned int?

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • This may not solve all of your FxCop problems. I wouldn't be surprised if it told you that you should have a `None` member for all `[Flags]` enums. This seems like a very good place for an inline warning suppression with a comment about why you can't change the code. – Cody Gray - on strike Aug 08 '12 at 22:06
  • @Cody: Yes, I might end up doing that. – Billy ONeal Aug 08 '12 at 22:07

2 Answers2

8

Use the unchecked keyword here and you will be ok; the persisted bit pattern will be what you expect.

[Flags]
public enum Example
{
    Foo = 0x00000001,
    Bar = unchecked((int)0xC0000000);
} 
Monroe Thomas
  • 4,962
  • 1
  • 17
  • 21
0

Thomas's answer is 100% right, but I will add my three cents to it: instead of "fixing" your code which was already correct, you can always supress FxCop rules for some part of the code.

See How do I suppress FxCop rule 'DoNotCatchGeneralExceptionTypes' with SupressMessage? and it's accepted answer for example of use. Everything is set in the code and .csproj, so all such supression settings stay with the code when given away to the end client.

Community
  • 1
  • 1
quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
  • FxCop is there to find "correct" things which are bad ideas. If I suppress everything it kinda defeats the point. (To be clear, by "as FxCop clean as possible" what I'm doing is going through our suppressions and removing them) – Billy ONeal Aug 08 '12 at 22:09
  • 1
    My previous comment was a nonsense. What I was trying to say was, that the .Net itself is full of FxCop suppressions, check for yourself. Almost for every rule there is a case when the rule is "generally right" but actually unwanted. I think that yours compatibility with the underlying storage is the case. Of course, unless your enum and assembly has t obe used in languages that does not have "unsigned"s. Anyways, if you have to do it - do it :) Thomas's answer is great. I just added mine for completeness. – quetzalcoatl Aug 08 '12 at 22:16