C # use the "|" operation to combine multiple

Source: Internet
Author: User

A few days ago, I saw a piece of code int I = GetCount (para1 | para2). It seems a bit strange. How can I transmit a parameter with an OR operation? In fact, there is something special here, I checked the materials from various parties and found out what I had to do in the QQ group. This operation can be used for multiple combinations. For example, when designing A permission, you will certainly design it like this: Permission A, what to do, permission B, what to do, permission C, and what to do... this Defines permissions. When calling A processing function, we can directly upload the corresponding permission Id. If user A has the permission, I must explicitly upload the Id of permission, user B has B permission. It seems reasonable to pass the Id of B permission. However, what if user C has both the and B permissions? You may say that I have set the AB permission when defining the permissions, in this way, all users with permissions A and B can pass the Id of AB '. I thought so at the beginning. However, this practice is not recommended, it is also not clever. If a user also has the ABC permission, BC permission, or CD permission, then I don't need to do anything, so I have been defining the type for it. We recommend that you use the "|" operation to combine parameters and split them with the "&" operation. In this way, callers can combine them at will. For processors, then, only the basic single processing "&" judgment can be defined. Example: Define an enumeration to indicate the permission type: copy the code public enum Privilege {Read = 0x01, Write = 0x02, Add = 0x04, delete = 0x08,} copy the Code to define a handler: copy the code public void GetPrivilege (Privilege pri) {if (pri & Privilege. read) = Privilege. read) {// do something} if (pri & Privilege. write) = Privilege. write) {// do something} if (pri & Privilege. add) = Privilege. add) {// do something} if (pri & Privilege. delete) = Privilege. del Ete) {// do something} copy the code and call it elsewhere: copy the code... getPrivilege (Privilege. read); GetPrivilege (Privilege. write); GetPrivilege (Privilege. add); GetPrivilege (Privilege. delete); GetPrivilege (Privilege. read | Privilege. write); GetPrivilege (Privilege. write | Privilege. add); GetPrivilege (Privilege. read | Privilege. write | Privilege. add );... copy the code and you will see that each call is executed accurately. Why is this possible? Based on the defined enumeration, the corresponding values are 0x010x020x040x08. Why can't I define 0x01,0x02,0x03,0x04, this is dependent on the "|" and "&" operations I mentioned at the beginning. The "|" or operation is 1 if the value of one is 1, "&" and the operation are both 1 to 1. Convert 0x01,0x02,0x04 and 0x08 to a binary system of 0001001001001000.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.