Dinny
Dinny

Reputation: 475

What does the operator |= mean in C#?

I could see this operator |= used in some sample code in my project. The exact code is given below

DocumentRetrievalOptions docRetrievalOptions = DocumentRetrievalOptions.ByTargetJurisdiction;    
    docRetrievalOptions |= DocumentRetrievalOptions.ByUniqueId;

Where 'DocumentRetrievalOptions' is of type enum.

It would be of great help, if some one lets me know, what really does this mean.

Upvotes: 3

Views: 449

Answers (5)

Péter
Péter

Reputation: 2181

Also read this: http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx It's really usefull if you wish to use this operator in this way.

Upvotes: 1

Jesus Ramos
Jesus Ramos

Reputation: 23268

Usually values in an enumeration are used as flags, the |= or "or equals" operator simply takes the bit representation of these values and does a bitwise OR on them. This way you "enable" another feature or flag of the enumeration (in this case the retrieval options for a document os its either by target jursisdiction OR unique id).

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324630

It is a shortcut for:

docRetrievalOptions = docRetrievalOptions | DocumentRetrievalOptions.ByUniqueId;

| being the bitwise-OR operator. In this way it works similarly to +=, -= and other operators in this style.

Upvotes: 1

Jesse Emond
Jesse Emond

Reputation: 7480

It applies the * bitwise or* operator (|) to both operands and stores the result inside docRetrievalOptions.

It is the same as docRetrievalOptions = docRetievalOptions | DocumentRetrievalOptions.ByUniqueId;

It interprets the enumeration as an int and then does the operation.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726569

It is a Bitwise/Logical OR - assign operator. A |= B; is the same as A = A | B;

Since DocumentRetrievalOptions is an enum, in your case |= performs a bitwise operation.

Upvotes: 6

Related Questions