23

Possible Duplicate:
C# int to enum conversion

Is it somehow possible to convert an int to a flag combination enum? So, if

[Flags]
public enum Foo {a = 0x80,
                 b = 0x40,
                 c = ...,
                 ...
                 h = 0x1,
                 i = 0};

is it OK (or somehow possible) to do

Foo fooInstance = (Foo)6;

so that fooInstance would be 00000110?

Thanks!

Community
  • 1
  • 1
john
  • 3,683
  • 6
  • 24
  • 39
  • 1
    What happened when you tried it? – dtb Oct 07 '11 at 18:02
  • @VirtualBlackFox: the flag part is a different aspect. – Gert Arnold Oct 07 '11 at 18:10
  • @GertArnold `FlagsAttribute` is AFAIK only used in the `Enum.ToString` method and nothing change in the compiler whenever it's there or not. It's name can't even be found in the language specification document. value -> enum is defined as value -> enum_underlaying_type without any exception. So while the OP didn't know that it's question was a duplicate, it is. – Julien Roncaglia Oct 07 '11 at 20:20

1 Answers1

14

Yes.

That works just fine. Flags attribute or not.

Rangoric
  • 2,699
  • 1
  • 17
  • 18