What does the operator do | = in C #?

Viewing a sample code from C # 4.0 in a nutshell I came across some interesting operators with enumerations

[Flags]
public enum BorderSides { Left=1, Right=2, Top=4, Bottom=8 }

...
BorderSides leftRight = BorderSides.Left | BorderSides.Right;
...

BorderSides s = BorderSides.Left;
s |= BorderSides.Right;
...

s ^= BorderSides.Right; 

Where is this described elsewhere?

UPDATE

Found a forum post related to this

+3
source share
3 answers

|= is bitwise or destination.

This statement:

BorderSides s = BorderSides.Left;
s |= BorderSides.Right;

coincides with

BorderSides s = BorderSides.Left;
s = s | BorderSides.Right;

This is usually used in enumerations as flags to store multiple values ​​in a single value, such as a 32-bit integer (default size enumin C #).

It looks like an operator +=, but instead of making an addition, you do a bitwise or.

+8
source

- # | =

+3

Source: https://habr.com/ru/post/1783350/


All Articles