Design status using Enumeration

Source: Internet
Author: User
When I was in a company, it looked very bad, that is, the state of an object is represented by Int. Personal perception mainly has the following shortcomings.
Advantages of enumeration:
1. Although int is a strong type in C #, Int Is not a strong type of business logic, but enumeration can implement a strong type of business logic.
For example, the status of an order is {New Order, payment, delivery, and completion}. If enumeration is used, it can be determined that it can safely represent these statuses, if you use a number directly, this number may not correctly represent these states.
2, Program Code The readability is worse. when others are reading or modifying the data, they should constantly look at the explanation of the number.
Status. New tickets should be easier to understand than 0
If (status = orderstatus. Finnish) is easier to understand than if (status = 3...
3. The status cannot be combined, and the status changes a lot.
For example, for a record to be approved, the review result is (Pass and reject), and then the review level A, Level B, level C, Level D, and Level E are to be approved.
2*5 = 10 numbers are required. The enumeration status requires 2 + 5 = 7, and can be used for calculation.
\\...... Other ideas are coming to supplement ·····

Enumeration operation:

Enumeration can be converted to int, so the budget of enumeration is actually a bit operation of Int.

Enumeration or calculation (|): I was cheated by the teacher when I was studying C. I divided the numbers into two hexadecimal notation, and then alignment the next bit on the paper for or operation. Then convert it to a decimal number. In fact, there are some tips. You have learned a set. We can think of a number as a set of 2 ^ n (n> = 0) elements. For example, 7 = {1, 2, 4}
21 = {16 + 4 + 1}. In this way, it is easy to convert to a binary system. 21 is (10101 ). After understanding the above, it is easy to calculate the number or operation. Or the budget is actually a merge or an operation in our set. For example, 7 | 21 = {16, 4, 2, 1} = 23. If it is 2 ^ n (n> = 0), it is easier to calculate, that is, to add the two numbers. For example, 128 | 64 = 128 + 64 = 192

Enumeration and budget (&): enumeration and calculation are actually the transactions in the mathematical set ). For example, 21 & 7 = {5,128} = & 64 = {} = 0

Exclusive or operation of enumeration (^): the exclusive or operation of enumeration is actually the non-intersection (Union-intersection) of the intersection ). For example, 21 ^ 7 = {16, 2} = 18 is simply (21 | 7)-(21 & 7) = 18

Use of enumeration operations:
For example, for a record to be approved, the review result is (Pass and reject), and then the review level A, Level B, level C, Level D, and Level E are to be approved.
Now it indicates a C-level rejection: status. Denied | status. c
How to query Level D through is status & (status. Allow | status. c) = (status. Allow | status. c)

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.