Connect with Expertise | Find Experts, Get Answers, Share Insights

FlagsAttribute

 
Posts: n/a
#1: Nov 15 '05
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?



Miha Markic [MVP C#]
 
Posts: n/a
#2: Nov 15 '05

re: FlagsAttribute


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

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


 
Posts: n/a
#3: Nov 15 '05

re: FlagsAttribute


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:uvWkM3x6DHA.1852@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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
>
> <discussion@discussion.microsoft.com> wrote in message
> news:ODvFBqx6DHA.3288@TK2MSFTNGP11.phx.gbl...[color=green]
> > If I dont specify the [Flags] attribute on an enum to be used as a[/color]
> bitfield[color=green]
> > declared as 0,1,2,4,8... and so on, and use it as one normally would[/color][/color]
with[color=blue]
> |[color=green]
> > and & operators, would it behave differently?
> >
> >[/color]
>
>[/color]


Daniel O'Connell [C# MVP]
 
Posts: n/a
#4: Nov 15 '05

re: FlagsAttribute


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.

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


Jon Skeet [C# MVP]
 
Posts: n/a
#5: Nov 15 '05

re: FlagsAttribute


<discussion@discussion.microsoft.com> wrote:[color=blue]
> Ok so it just affects the ToString() method?
>
> I thought it affected the & and | operators.[/color]

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

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

re: FlagsAttribute


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]" <skeet@pobox.com> wrote in message
news:MPG.1a8b00a0f03b361998a065@msnews.microsoft.c om...[color=blue]
> <discussion@discussion.microsoft.com> wrote:[color=green]
> > Ok so it just affects the ToString() method?
> >
> > I thought it affected the & and | operators.[/color]
>
> Not in C#, but another language *may* decide to only allow the & and |
> operators to work with enums declared as Flags.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]


Closed Thread