简体   繁体   中英

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.

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

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

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.

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

另请阅读: http//msdn.microsoft.com/en-us/library/system.flagsattribute.aspx如果您希望以这种方式使用此运算符,这非常有用。

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM