Search code examples
c#.netoperatorsassignment-operator

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?


Solution

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