If I'm using something like this:
xr.Settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; What precisely is the |= accomplishing?
|= is a shortcut for OR'ing two values together and assigning the result to the first variable.
xr.Settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; Is equivalent to:
xr.Settings.ValidationFlags = xr.Settings.ValidationFlags | XmlSchemaValidationFlags.ReportValidationWarnings; | is the OR operator in C#, so the code above effectively sets the ReportValidationWarnings flag on the value xr.Settings.ValidationFlags.
1001 1101 and you OR it with 1100 1011 you would get 1101 1111. If a bit it set in either operand, then it is set in the result. The shorthand of |= just states that the left operand of the OR is the operand to the left of the |=.bool b = true; b |= false; and yet b still returns true. I feel stupid, but I'm not seeing how that translates into what's happening above.That is a compound assignment. Essentially you are doing:
xr.Settings.ValidationFlags = xr.Settings.ValidationFlags | XmlSchemaValidationFlags.ReportValidationWarnings; Which will essentially add XmlSchemaValidationFlags.ReportValidationWarnings to the set of flags contained in xr.Settings.ValidationFlags.
As others have mentioned, a |= b is shorthand for a = a | b, in much the same way as a += b is short for a = a + b. Now what does the | operator do? It can be overloaded, but the general use of it is bit-wise OR. It's similar to the || operator, but it works on a bit by bit basis (think of each bit as a boolean):
false || true is true
0100 | 0110 is 0111
Then the last thing is that one of the classic ways to pass a bunch of boolean flags is to encode them in an integer. A single 32 bit integer can hold 32 individual flags, one per bit. to set a flag, you set the corresponding bit to 1.
So 0000 has no flags set, while 1001 has flags at position 1 and 4 set.
Then |= is a convenient way to set a specific flag.
int my_flags = 0;
my_flag |= validate_flag;
driis' answer is correct.
|= is the same as using the | operator in a conditional test, as in the following examples:
xr.Settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings is semantically identical to
xr.Settings.ValidationFlags = xr.Settings.ValidationFlags | XmlSchemaValidationFlags.ReportValidationWarnings | vs ||One additional tidbit I found to be useful was the difference between the logical OR (|) and the conditional OR (||):
The logical OR will always evaluate both operands, even if the first is true. The conditional OR will only evaluate the second if the first is false, effectively short-circuiting if the execution of the second operand is unnecessary to determine the final result.