What type of parameter argument allows you to use the '|' pipe in the argument list?

Possible duplicate:
What is bitwise or | operator?

new Font(textBox1.Font, FontStyle.Bold | FontStyle.Italic);

What does the signature of the method that accepts this constructor call mean?

I never knew I could use '|' statement in a method call. I would like to know more about this.

What is the English word for '|' operator? (I don’t even know how to do this, since I don’t know a word to describe it)

When it is used in a method, how to explain it to another developer?

Would you recommend including this operator in your tricks bag?

Does the operator have any special reservations?

+6
source share
1 answer

The signature of the acceptance method is as follows:

public Font(Font prototype, FontStyle newStyle)
{
    ...
}

| ( ) , , . , FontStyle - enum, FlagsAttribute. FontStyle:

[Flags]
public enum FontStyle
{
    Bold = 1,
    Italic = 2,
    Regular = 0,
    Strikeout = 8,
    Underline = 4
}

, FontStyle.Bold | FontStyle.Italic, :

FontStyle.Bold                    = 1 = 00000001
FontStyle.Italic                  = 2 = 00000010
                                        ========
FontStyle.Bold | FontStyle.Italic = 3 = 00000011

style, , , (&). , , , :

FontStyle myStyle = FontStyle.Bold | FontStyle.Italic;
bool isBold = (myStyle & FontStyle.Bold) == FontStyle.Bold;

, Bold Font , FontStyle.Bold , , , :

public bool Bold
{
    get
    {
        return ((this.Style & FontStyle.Bold) != FontStyle.Regular);
    }
}

, .NET Framework 4, Enum.HasFlag() . , ( # 6):

public bool Bold => this.Style.HasFlag(FontStyle.Bold);
+7

All Articles