Enumeration in C # with bitwise OR operator an enumeration variable is stored in multiple values

Source: Internet
Author: User

Before we defined an enumeration type and then defined an enumeration variable, the enumeration variable could only be a value in the type, and now we want a variable to represent multiple values:

When I see theenumeration section of the Pro Net 2.0 Windows Forms and Custom Cortrols in C # today, I find that we need to combine multiple values in an enumeration, and see that the "|" is used. operator, originally did not pay much attention to, today thought for a moment why use "|" It?
In MSDN, you see the phrase, " Define enumeration constants with a power of 2 (that is, 1,2,4,8, and so on.) This means that the individual flags in the combined enumeration constant do not overlap. ”
So I wrote an example:

[FlagsAttribute] plus he enumerates variables to do bits or actions
Enum Colors_1
{
Red = 1, Green = 2, Blue = 4, Yellow = 8
};
Test
private void Button1_Click (object sender, EventArgs e)
{
Colors_1 color_1 = colors_1.red | Colors_1.green | Colors_1.blue
| Colors_1.yellow;

String strresult = color_1.tostring () + "" + ((int) color_1)
. ToString ();
MessageBox.Show (strresult);
}

Output Result:


Hey! 1 + 2 + 4 + 8 = 15 just equals 15, is that a coincidence?
Show it all, ease!

Try to write another example:

[FlagsAttribute]
Enum Colors_2
{
Red = 1, Green = 2, Blue = 3, Yellow = 4
};
Test
private void Button1_Click (object sender, EventArgs e)
{
Colors_2 color_2 = colors_2.red | Colors_2.green | Colors_2.blue
| Colors_2.yellow;

String strresult = color_2.tostring () + "" + ((int) color_2). ToString ();
MessageBox.Show (strresult);
}


Output Result:

Dizzy, why didn't you show all the colors?
Hey! 3 + 4 = 7 Displays exactly two colors with an enumeration value of 3,4

Write another example?

Test
private void Button1_Click (object sender, EventArgs e)
{
Colors_1 C = (colors_1) enum.parse (typeof (Colors_1), "7");
MessageBox.Show (c.tostring () + "" + ((int) c). ToString ());
}

Output Result:

It will automatically convert to the corresponding enumeration value, bad!

Let me add a value with an enumeration of 7:

[FlagsAttribute]
Enum Colors_1
{
Red = 1, Green = 2, Blue = 4, Yellow = 8, Seven = 7
};
Test
private void Button1_Click (object sender, EventArgs e)
{
Colors_1 C = (colors_1) enum.parse (typeof (Colors_1), "7");
MessageBox.Show (c.tostring () + "" + ((int) c). ToString ());
}


Output Result:

The MSDN statement is confirmed, only the enumeration value is set to 0,2,4,8 .... This will only stack, the enumeration will automatically determine the current value, if the enumeration has this value, of course, the value is shown; If you do not have to do a match with the addition to see that the number of the sum is exactly the enumeration value, but if a few numbers add up equals this value how to do? Haven't met yet, now this is my understanding, hope Daniel some advice!

Use:::::

[Flags]//Note flags
public enum MyColor
{
None=0, Red=1,green=2,blue=4,yellow=8
};

private void Form1_mouseclick (object sender, MouseEventArgs e)
{

MyColor My=mycolor.none;
Add to

if (red)

my=my|mycolor.red;

if (green)

my=my|mycolor.red;

........

Extraction

if (my&mycolor.none=mycolor.none)? Ture:false;

if (my&mycolor.none=mycolor.red)? Ture:false;

String nnn = mmm. ToString ();

MessageBox.Show (NNN);

}

Enumeration in C # with bitwise OR operator an enumeration variable is stored in multiple values

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.