Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is happening when you use the |= operator in C#?

If I'm using something like this:

xr.Settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;

What precisely is the |= accomplishing?

like image 411
elucid8 Avatar asked Dec 19 '12 18:12

elucid8


2 Answers

|= 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.

like image 166
driis Avatar answered Nov 15 '22 00:11

driis


In this case you are setting XmlSchemaValidationFlags.ReportValidationWarnings flag in your ValidationFlags.

|= is generally bitwise or operator, in case of Flags it's used to set flag.

like image 41
Zbigniew Avatar answered Nov 14 '22 23:11

Zbigniew