C Sharp Flags: Difference between revisions

From Leo's Notes
This page was last edited on 24 February 2017, at 19:14.
No edit summary
m Text replacement - "</code>" to "}}"
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Defining Flags ==
== Defining Flags ==
[Flags]
<syntaxhighlight lang="csharp" line start="1" enclose="div">
public enum  State {
[Flags]
    Idle = 0x01,
public enum  State {
    Available = 0x02,
  Idle = 0x01,
    InService = 0x04,
  Available = 0x02,
   
  InService = 0x04,
    Accelerating = 0x08,
 
    Decelerating = 0x10,
  Accelerating = 0x08,
    Stopped = 0x20,
  Decelerating = 0x10,
}
  Stopped = 0x20,
}
</syntaxhighlight>


Notice that the flags are given values of powers of two.
Notice that the flags are given values of powers of two.
Line 18: Line 20:
Use the bit-wise OR operation to set (add) a flag.
Use the bit-wise OR operation to set (add) a flag.


State state = State.Available;
<syntaxhighlight lang="csharp" line start="1" enclose="div">
state = state | State.Stopped;
State state = State.Available;
state = state | State.Stopped;
</syntaxhighlight>
   
   
<code>state</code> now has both Available and Stopped. The actual value of <code>state</code> is 0x22 (from 0x02 + 0x22)
{{code|state}} now has both Available and Stopped. The actual value of {{code|state}} is 0x22 (from 0x02 + 0x22)


=== Removing a Flag ===
=== Removing a Flag ===
Use the bit-wise XOR (^) operation to unset (remove) a flag.
Use the bit-wise XOR (^) operation to unset (remove) a flag.


State state = State.Available | State.Stopped;
<syntaxhighlight lang="csharp" line start="1" enclose="div">
state = state ^ State.Available;
State state = State.Available | State.Stopped;
state = state ^ State.Stopped;
state = state ^ State.Available;
state = state | State.InService;
state = state ^ State.Stopped;
state = state | State.InService;
</syntaxhighlight>
   
   
<code>state</code> is now InService. The actual value of <code>state</code> is 0x04.
{{code|state}} is now InService. The actual value of {{code|state}} is 0x04.


=== Checking ===
=== Checking ===
Use the bit-wise AND operation to do checking.
Use the bit-wise AND operation to do checking.


State state = State.Available | State.Stopped;
<syntaxhighlight lang="csharp" line start="1" enclose="div">
State state = State.Available | State.Stopped;
if (state & State.Idle == State.Idle) {
 
    // This will fail as state is set to Available and Stopped.
if ((state & State.Idle) == State.Idle) {
}
  // This will fail as state is set to Available and Stopped.
}
</syntaxhighlight>


or use the {{code|HasFlag()}} method.


[[Category:C Sharp]]
[[Category:C-Sharp]]

Latest revision as of 19:14, 24 February 2017

Defining Flags

[Flags]
public enum  State {
   Idle = 0x01,
   Available = 0x02,
   InService = 0x04,
   
   Accelerating = 0x08,
   Decelerating = 0x10,
   Stopped = 0x20,
}

Notice that the flags are given values of powers of two.

Operations

Setting a Flag

Use the bit-wise OR operation to set (add) a flag.

State state = State.Available;
state = state | State.Stopped;

state now has both Available and Stopped. The actual value of state is 0x22 (from 0x02 + 0x22)

Removing a Flag

Use the bit-wise XOR (^) operation to unset (remove) a flag.

State state = State.Available | State.Stopped;
state = state ^ State.Available;
state = state ^ State.Stopped;
state = state | State.InService;

state is now InService. The actual value of state is 0x04.

Checking

Use the bit-wise AND operation to do checking.

State state = State.Available | State.Stopped;

if ((state & State.Idle) == State.Idle) {
   // This will fail as state is set to Available and Stopped.
}

or use the HasFlag() method.