473,402 Members | 2,064 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,402 software developers and data experts.

FlagsAttribute

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

Similar topics

7
by: Randy | last post by:
I can't seem to find a clear example on how to use the FlagsAttribute. In my example below shouldn't both message boxes show? Instead no messagebox displays. I know that .NET will auto-number...
0
by: Horia Tudosie | last post by:
Using Visual Studio 2003 This is to report a series of bugs regarding the FlagsAttribute and (independently) the usage of interfaces in Web applications. Let’s declare xColors type like: ...
5
by: Ken Varn | last post by:
The following code snippet will not compile. I get an error C2337 saying that the flags attribute is not found, however, if I remove the last namespace System from the namespace tree, then it...
2
by: Nicolas | last post by:
How do I set a FlagsAttribute to a run time created enum I really need a FlagsAttribute for the enum as it will be use to trigger multiple choice Thanks for the help Function BuildEnum(ByVal...
15
by: Fariba | last post by:
Hello , I am trying to call a mthod with the following signature: AddRole(string Group_Nam, string Description, int permissionmask); Accroding to msdn ,you can mask the permissions using...
2
by: Charles Jenkins | last post by:
I want to know if FontStyle.Bold is set in a font's Style property. MS's help file is full of lovely examples of how to OR flags together in order to set the property, but as usual, seems to be...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
2
by: Zytan | last post by:
I know you can use enums as a bit field with , but, when I use an enum without , I can still combine one or more of them. There's no error in doing so. I thought enums were strongly typed, such...
5
by: rajanipro | last post by:
Please explain to me the following C# program, which I came across in MSDN help: // enumFlags.cs // Using the FlagsAttribute on enumerations. using System; public enum CarOptions { ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.