Search code examples
c#windows-cenullreferenceexceptionenum-flags

What is the "|=" operator in C#?


In researching the cause of a bug, I came across this line of code:

Status |= (int)states.Reading;

What is the "|=" operator in C#?

"Status" is defined thusly:

public static int Status 

...with an accessor and mutator (or "getter" and "setter"), while "states" is defined this way:

[Flags]
public  enum states

Solution

  • It's the "bitwise logical OR" operator, as defined here.

    x |= y is equivalent to x = x | y

    Also, if you want to learn more about the "|" operator itself, you can do so here.