0

I frequently read code like this:

System.Diagnostics.DebuggableAttribute.DebuggingModes.DisableOptimizations | System.Diagnostics.DebuggableAttribute.DebuggingModes.EnableEditAndContinue | System.Diagnostics.DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | System.Diagnostics.DebuggableAttribute.DebuggingModes.Default)

in C# program,sometimes parameter like this,what's this meanning ?

I have searched from google,but has no valuable answer,the '|' can not property parsing in Google engine,maybe i use the wrong way when searching.

Dolphin
  • 29,069
  • 61
  • 260
  • 539

5 Answers5

4

'|' is the bitwise or operator, in this case it is used to create an enum value with all the given bits set.

DebuggingModes is a bitflag enumeration - this means each bit can indicate a flag and a single DebuggingModes value can be used to signal multiple flags.

Enums can be made bitflags using the BitFlagsAttribute:

[FlagsAttribute]
public enum DebuggingModes
{
   Default = 0,
   DisableOptimizations = 1,
   EnableEditAndContinue = 2,
   ...
}
Lee
  • 142,018
  • 20
  • 234
  • 287
  • 1
    It depends. If you're comparing Booleans, it's a non-short-circuiting OR comparison (`||` is short-circuiting). For integral types (which includes enums), it's indeed the bitwise OR. – David S. Dec 08 '13 at 13:39
3

In this case it seems that it is a Flags enum

[Flags]
public enum Types
{
    None = 0,
    Type1 = 1,
    Type2 = 2,
    Type3 = 4,
}

So

Types someType = Types.Type1 | Types.Type2;

Would mean that it has both types.

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
1

It is a bitwise OR operator iin C#. Here it is used to create an enum value with all the given set of bits.

Binary | operators are predefined for the integral types and bool. For integral types, | computes the bitwise OR of its operands. For bool operands, | computes the logical OR of its operands; that is, the result is false if and only if both its operands are false.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

It is a logical or operator. see here for a complete explanation.

Main explanation in the doc:

Binary | operators are predefined for the integral types and bool. For integral types, | computes the bitwise OR of its operands. For bool operands, | computes the logical OR of its operands; that is, the result is false if and only if both its operands are false.

davidrac
  • 10,723
  • 3
  • 39
  • 71
0

when | is used as || it is OR operator and here it used as Enum values

donstack
  • 2,557
  • 3
  • 29
  • 44