By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 954 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Apr 10 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Zytan" <zy**********@gmail.comwrote in message
news:11*********************@b75g2000hsg.googlegro ups.com...
>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

Apr 10 '07 #2

P: n/a
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

Apr 10 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.