Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, what does '|=' do?

Tags:

operators

c#

I can't seem to Google it - doesn't appear to like the syntax in the search string. Thank you for the help.

like image 686
naspinski Avatar asked Mar 07 '12 21:03

naspinski


2 Answers

This is a bit wise assignment. It's roughly shorthand for the following

x |= y;
x = x | y;

Note: It's not truly the above because the C# spec guarantees the side effects of x only occur once. So if x is a complex expression there is a bit of fun code generated by the compiler to ensure that the 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
like image 94
JaredPar Avatar answered Oct 26 '22 03:10

JaredPar


The expression a |= b is equivalent to the assignment a = a | b, where | is the bitwise OR operator.*

* Not entirely, but close enough for most purposes.

like image 41
Ry- Avatar answered Oct 26 '22 05:10

Ry-