Digging up a series of things we don't use in C # (5)--flagattribute

Source: Internet
Author: User

Speaking of FlagsAttribute, from a few days ago saw a small piece of code, probably means that according to the flight policy to return which shipping methods are available, depending on whether these are available

To hide or open the interface of the relevant distribution method, if you have booked a ticket may know that there are many ways of delivery, such as "No need to print itinerary (PJN)", "Airport pickup",

"City Delivery (CND)", "Express (EMS)" and so on.

According to the above logic can be known, the distribution method is there are two states, available and not available, in the logical implementation, it is easy to think of a bit bit, each bit represents a distribution method,

0 means not available, 1 is available, so the flight interface just gives a number, I just need to determine which of the bit bits is 1.

For example, use the 8-bit byte field as an example:

Can see, Express (EMS) is not available, then how to judge, in fact, it is OK 17&32, if 32, then the description is available, 0 is not available, other

Can be judged by the same means.

The last time you saw the code logic is this, but after all, in a team, horizontal uneven, with pure numbers to &,|,^, at least not so easy to understand, if

With an enumeration, it might be more perfect.

The enumeration, in fact, is the compiler to our syntax sugar, in essence, is a inherit under the enum type of a const field, since it is a const, that

Be born with (+,-,*,/^,| &) Such a conventional mathematical operation.

As an example:

1 [Flags]2     enumDeliver:byte3     {4CND =0x01,5PJS =0x02,6SND =0x04,7PJN =0x08,8Airport =0x16,9EMS =0x32Ten}

Then look at the IL code generated by the enum above.

Some people may ask, "uint8" Here is what happened, in fact, this is hidden behind the enumeration type of the real primitive type, you can use the Getunderlyingtype to get.

Typically, an enumeration can only show one state, so if you let the enumeration show multiple states, this time you can use Flagattribute to mark the flags to handle the enumeration.

To do a powerful combination function.

For example: 17 of the flight policy returns, we know that airport and CND are available, and if we use the flagattribute tag, we don't have to

The 17 directly into the enumeration.

See, after enumeration conversion, it is possible to understand the programmer and record log above, more convenient analysis and tracking.

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

Digging up a series of things we don't use in C # (5)--flagattribute

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.