In C #, what does' | = '?

I can't seem to google it, it's like the syntax in the search bar. Thanks for the help.

+3


source to share


8 answers


This is a bit of a wise assignment. This is roughly shortened for the following

x |= y;
x = x | y;

      



Note. This is not the case because the C # specification ensures that side effects x

only appear once. Therefore, if it x

is a complex expression, a fun bit of code is generated for the compiler to ensure that side effects only happen once.

Method().x |= y;
Method().x = Method().x | y;  // Not equivalent

var temp = Method();
temp.x = temp.x | y;  // Pretty close

      

+14


source


Expression is a |= b

equivalent to assignment a = a | b

, where |

is the bitwise OR operator. *



* Not quite, but close enough for most purposes.

+3


source


He likes +=

it but with a binary OR

int x = 5;
x |= 6;  // x is now 7: 5 | 6

      

You can also do the other, such as &=

, /=

, *=

, etc. Almost any binary (two arguments) operator

+2


source


In C and other languages ​​that follow C syntactic conventions such as C ++, Perl, Java, and C #, (a | b) denotes bitwise or; while the double vertical bar (a || b) denotes (short-circuited) logical or.

0


source


a |= b

semantically matches a = a | b

0


source


| = is a bitwise assignment operator. Check out the msdn documentation here http://msdn.microsoft.com/en-us/library/h5f1zzaw(v=vs.100).aspx

0


source


you will find the answer here at msdn

x |= y

      

coincides with

x = x | y

      

0


source


0


source







All Articles