enums as bitfields, combining them works even without [FlagsAttribute]

Z

Zytan

I know you can use enums as a bit field with [FlagsAttribute], but,
when I use an enum without [FlagsAttribute], I can still combine one
or more of them. There's no error in doing so. I thought enums were
strongly typed, such that you need an explicit cast from an int to get
a wrong value into an enum.

class Program
{
enum RGB0
{
None = 0,
Blue = 1,
Green = 2,
Red = 4
}

[FlagsAttribute]
enum RGB1
{
None = 0,
Blue = 1,
Green = 2,
Red = 4
}

[FlagsAttribute]
enum RGB2
{
None = 0,
Blue = 1,
Green = 2,
Cyan = 3,
Red = 4,
Purple = 5,
Yellow = 6,
White = 7
}

static void Main(string[] args)
{
Console.WriteLine(new string('-', 70));

RGB0 color0 = RGB0.Green;
Console.WriteLine(color0);
color0 = RGB0.None;
Console.WriteLine(color0);
color0 = RGB0.Red;
Console.WriteLine(color0);
color0 = RGB0.Red | RGB0.Green;
Console.WriteLine(color0);
color0 = RGB0.Red | RGB0.Green | RGB0.Blue;
Console.WriteLine(color0);

Console.WriteLine(new string('-', 70));

RGB1 color1 = RGB1.Green;
Console.WriteLine(color1);
color1 = RGB1.None;
Console.WriteLine(color1);
color1 = RGB1.Red;
Console.WriteLine(color1);
color1 = RGB1.Red | RGB1.Green;
Console.WriteLine(color1);
color1 = RGB1.Red | RGB1.Green | RGB1.Blue;
Console.WriteLine(color1);

Console.WriteLine(new string('-', 70));

RGB2 color2 = RGB2.Green;
Console.WriteLine(color2);
color2 = RGB2.None;
Console.WriteLine(color2);
color2 = RGB2.Red;
Console.WriteLine(color2);
color2 = RGB2.Red | RGB2.Green;
Console.WriteLine(color2);
color2 = RGB2.Red | RGB2.Green | RGB2.Blue;
Console.WriteLine(color2);
}
}

// OUTPUT:
//
----------------------------------------------------------------------
// Green
// None
// Red
// 6
// 7
//
----------------------------------------------------------------------
// Green
// None
// Red
// Green, Red
// Blue, Green, Red
//
----------------------------------------------------------------------
// Green
// None
// Red
// Yellow
// White

Zytan
 
N

Nicholas Paldino [.NET/C# MVP]

Zytan,

That would be incorrect. If you have an underlying type for an enum
(int, long, etc, etc) you can cast it to enumeration that uses that
underlying type. There is no check by the runtime or the compiler to make
sure that the value is a valid value in the enumeration.

If you need to check to see if the value exists in the enumeration, then
you need to call the static IsDefined method on the Enum class.

Hope this helps.
 
Z

Zytan

That would be incorrect. If you have an underlying type for an enum
(int, long, etc, etc) you can cast it to enumeration that uses that
underlying type. There is no check by the runtime or the compiler to make
sure that the value is a valid value in the enumeration.

Ok, so, you cannot implicitly cast an int into an enum, even if its
udnerlying type is int. That's strong typing. You need explicit
casting to do so. That's what I thought.

But, you can OR two enum values together to get a wrong one, and
compiler doesn't check for this, even though, unless its a bit-field,
it makes no sense (a lot of the time) to do such a thing.
If you need to check to see if the value exists in the enumeration, then
you need to call the static IsDefined method on the Enum class.

Ok, yes, this does help. Thanks

Zytan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top