C # bit domain [flags]

Source: Internet
Author: User

Bit fields are mainly used in. net when there are multiple mixed states for a thing. A single enumeration is used when the thing only has a single attribute. To better implement the mixed state, we can add the flags label to the enumeration. The following example is used in this article:

[Flags] public Enum week {[description ("Monday")] Monday = 1 <0, [description ("Tuesday")] Tuesday = 1 <1, [description ("Wednesday")] Wednesday = 1 <2, [description ("Thursday")] tursday = 1 <3, [description ("Friday")] friday = 1 <4, [description ("Saturday")] Saturday = 1 <5, [description ("Sunday")] Sunday = 1 <6}

Operators supported by bit Fields

1. "|": calculates the Union set on both sides (elements are added and the same element only appears once)

Week week = Week.Tuesday | Week.Monday | Week.Monday;MessageBox.Show(Convert.ToString(week));

The result of this Code is Monday and Tuesday.

2. "&": Indicates whether one of the two sides is another subset. If it is a subset, otherwise 0 is returned. (if one includes the other, the system returns the contained one. Otherwise, the system returns 0)

week = Week.Monday & week;MessageBox.Show(week.ToString());
And
week = week & Week.Monday;MessageBox.Show(week.ToString());

The results of the above two codes are the same. If the initial values of week are Monday and Tuesday, the returned results are Monday.

3. "^": removes the intersection of the two from the Union (merges two elements together. If two elements have common elements, remove this public element from the merged result)

week = (Week.Monday | Week.Wednesday)^ (Week.Tuesday | Week.Monday);MessageBox.Show(week.ToString());week = (Week.Monday | Week.Wednesday) ^ (Week.Tuesday | Week.Sunday);MessageBox.Show(week.ToString());

The two returned results are: Tuesday, Wednesday, Monday, Tuesday, Wednesday, and Sunday.

4. "~" : Indicates the inverse result. I do not know what the returned result should be. I will check it later. It is used together with "&", for example, removing an element.

week = Week.Tuesday | Week.Monday | Week.Wednesday;week = week &(~Week.Monday);MessageBox.Show(week.ToString());

The returned results are Tuesday and Wednesday.

Forward and reverse Conversion

When the above content already exists in the database, we may simply access numbers. For example, 1 indicates Monday, 3 indicates Monday, and Tuesday. We can easily obtain the stored content based on the values in the database. The Code is as follows:

week = Week.Monday  | Week.Tuesday;MessageBox.Show(Convert.ToString((int)week));week = (Week)Enum.Parse(typeof(Week), "10");MessageBox.Show(week.ToString());

Returned results: 3 and Tuesday, tursday

Get description tag content

Since we can add description to the value, we can get this content in the program. As for the purpose, let's take a look at it. Let's get something out and use it as needed, the following code is found on the Internet:

/// <Summary> /// read from the enumeration type and its features and return a key-Value Pair /// </Summary> /// <Param name = "enumtype"> type, the format of this parameter is typeof (Enumeration type to be read) </param> // <returns> key-Value Pair </returns> Public static namevaluecollection getnvcfromenumvalue (type enumtype) {namevaluecollection NVC = new namevaluecollection (); Type typedescription = typeof (descriptionattribute); system. reflection. fieldinfo [] fields = enumtype. getfields (); string strtext = string. empty; string strvalue = string. empty; foreach (fieldinfo field in fields) {If (field. fieldtype. isenum) {strvalue = (INT) enumtype. invokemember (field. name, bindingflags. getfield, null )). tostring (); object [] arr = field. getcustomattributes (typedescription, true); If (ARR. length> 0) {descriptionattribute AA = (descriptionattribute) Arr [0]; strtext = AA. description;} else {strtext = field. name;} NVC. add (strtext, strvalue) ;}return NVC ;}

Thanks:
Http://blog.csdn.net/collinye/archive/2008/10/07/3027536.aspx
Http://www.cnblogs.com/c2303191/articles/1103475.html

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.