473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
2 2037
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
23
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
6
by: GalenTX | last post by:
I am looking for opinions on a possible approach to coping with bitfields in a heterogenous environment. We've got a bunch of legacy code which maps to hardware using bitfields. The code was...
3
by: Mike Scott | last post by:
What is the best way to index an array by an enum. Pascal has a nice feature in that arrays can be indexed by an enum, for example type ButtonType = ( Left, Middle, Right ) ; var buttons :...
5
by: DMitchell | last post by:
Is there any way of getting a public enumeration to be generated into a web service proxy?
3
by: TedKennedyMurderedHisPregnantMistress.dwpj65 | last post by:
Hello, I'm attempting to define a class that has a bitfield as a static member. The definition seems ok, but I'm at a loss as to how to declare it: class Test { static struct TestBitFields {...
9
by: userblue | last post by:
Hi Does anyone know if there is a way to define what is effectively a single globally visible, enumerated list whilst actually defining the entries across several different modules? or somehow do...
2
by: James Brown | last post by:
All, Suppose I have the following enum and structure definitions: enum e { ename1, ename2, ename3, ename4 }; struct s
9
by: cman | last post by:
Who can explain to me what bitfields are and how to use them in practice? I would need a fairly detailed explaination, I would be a newbie to advanced C programming features. What are the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.