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

FlagsAttribute

P: n/a
If I dont specify the [Flags] attribute on an enum to be used as a bitfield
declared as 0,1,2,4,8... and so on, and use it as one normally would with |
and & operators, would it behave differently?
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

No.
FlagsAttribute has influence only on output, check this code (toggle the
Flags attribute to see the difference):
class Class1

{

[Flags]

public enum Flagator

{

One = 1,

Two = 2

}

[STAThread]

static void Main(string[] args)

{

Flagator ft = Flagator.One | Flagator.Two;

Console.WriteLine(ft.ToString());

Console.ReadLine();

}

}
--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

<di********@discussion.microsoft.com> wrote in message
news:OD**************@TK2MSFTNGP11.phx.gbl...
If I dont specify the [Flags] attribute on an enum to be used as a bitfield declared as 0,1,2,4,8... and so on, and use it as one normally would with | and & operators, would it behave differently?

Nov 15 '05 #2

P: n/a
Ok so it just affects the ToString() method?

I thought it affected the & and | operators.

"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:uv**************@TK2MSFTNGP10.phx.gbl...
Hi,

No.
FlagsAttribute has influence only on output, check this code (toggle the
Flags attribute to see the difference):
class Class1

{

[Flags]

public enum Flagator

{

One = 1,

Two = 2

}

[STAThread]

static void Main(string[] args)

{

Flagator ft = Flagator.One | Flagator.Two;

Console.WriteLine(ft.ToString());

Console.ReadLine();

}

}
--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

<di********@discussion.microsoft.com> wrote in message
news:OD**************@TK2MSFTNGP11.phx.gbl...
If I dont specify the [Flags] attribute on an enum to be used as a bitfield
declared as 0,1,2,4,8... and so on, and use it as one normally would

with |
and & operators, would it behave differently?


Nov 15 '05 #3

P: n/a
As far as the math goes, no. The bitwise operators will still work
correctly. However, the Flags attribute changes the behaviour of ToString()
so that it displays the proper set(all the set flags) instead of simply one
or a simple number.

There is no guarentee that bitwise operators will work on enums without
flags in other languages.

<di********@discussion.microsoft.com> wrote in message
news:OD**************@TK2MSFTNGP11.phx.gbl...
If I dont specify the [Flags] attribute on an enum to be used as a bitfield declared as 0,1,2,4,8... and so on, and use it as one normally would with | and & operators, would it behave differently?

Nov 15 '05 #4

P: n/a
<di********@discussion.microsoft.com> wrote:
Ok so it just affects the ToString() method?

I thought it affected the & and | operators.


Not in C#, but another language *may* decide to only allow the & and |
operators to work with enums declared as Flags.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

P: n/a
I think thats how it should be for the operators, it makes no sense to use
bitwise operators on a non bitwise type.

I would also like to see some form of auto enumeration by base as an
attribute to it can automatically fill in 0,1,2,4,8 based on the order
unless I specify otherwise.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<di********@discussion.microsoft.com> wrote:
Ok so it just affects the ToString() method?

I thought it affected the & and | operators.


Not in C#, but another language *may* decide to only allow the & and |
operators to work with enums declared as Flags.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.