473,563 Members | 2,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4401
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.WriteLi ne(ft.ToString( ));

Console.ReadLin e();

}

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

<di********@dis cussion.microso ft.com> wrote in message
news:OD******** ******@TK2MSFTN GP11.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******** ******@TK2MSFTN GP10.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.WriteLi ne(ft.ToString( ));

Console.ReadLin e();

}

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

<di********@dis cussion.microso ft.com> wrote in message
news:OD******** ******@TK2MSFTN GP11.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********@dis cussion.microso ft.com> wrote in message
news:OD******** ******@TK2MSFTN GP11.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********@dis cussion.microso ft.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
<di********@dis cussion.microso ft.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.co m>
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
2685
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 enum's when omitted. <Flags()> Public Enum Permission Read Write Delete Rename Copy
0
1370
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: public enum xColors { Red = 1,
5
1301
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 compiles fine. What gives? #pragma once using namespace System; namespace TestA
2
1677
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 EnumName As String, ByVal args As Hashtable) As Type Dim ad As AppDomain Dim an As New AssemblyName Dim ab As AssemblyBuilder Dim mb As ModuleBuilder...
15
3063
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 pipe symbol .for example you can use something like this AddRole("My Group", "Test", 0x10000000|0x00000002);
2
886
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 missing the other half of the equation: How do you test the darned flags? These tests all give me a compiler errors in C#: FontStyle fs =...
34
11150
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 snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a...
2
2043
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 that you need an explicit cast from an int to get a wrong value into an enum. class Program { enum RGB0 {
5
5038
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 { SunRoof = 0x01,
0
7664
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7885
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6250
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.