什么类型的参数参数允许管道'|' 在参数列表中?

sap*_*ket 6 c#

可能重复:
按位或| 操作员呢?

new Font(textBox1.Font, FontStyle.Bold | FontStyle.Italic);
Run Code Online (Sandbox Code Playgroud)

接受此构造函数调用的方法签名是什么样的?

我从来不知道我可以使用'|' 方法调用中的运算符.我想了解更多相关信息.

'|'的英文单词是什么?运营商?(我甚至不知道如何谷歌它,因为我不知道一个词来形容它)

在方法中使用它时,如何向其他开发人员解释?

你会建议我把这个算子包含在我的技巧包里吗?

运营商是否有任何特殊警告?

Cᴏʀ*_*ᴏʀʏ 7

接受方法的签名看起来像:

public Font(Font prototype, FontStyle newStyle)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

|此上下文中的运算符(按位或)表示字体应为粗体和斜体.它的工作方式是这样的,因为它FontStyle是用一个enum装饰的FlagsAttribute.该FontStyle定义是:

[Flags]
public enum FontStyle
{
    Bold = 1,
    Italic = 2,
    Regular = 0,
    Strikeout = 8,
    Underline = 4
}
Run Code Online (Sandbox Code Playgroud)

所以当你说FontStyle.Bold | FontStyle.Italic,它是按位OR:

FontStyle.Bold                    = 1 = 00000001
FontStyle.Italic                  = 2 = 00000010
                                        ========
FontStyle.Bold | FontStyle.Italic = 3 = 00000011
Run Code Online (Sandbox Code Playgroud)

稍后,您可以测试style参数以查看使用其他按位运算符(&)设置哪些位.例如,如果您想查看上面的结果样式是否为粗体,您可以执行以下操作:

FontStyle myStyle = FontStyle.Bold | FontStyle.Italic;
bool isBold = (myStyle & FontStyle.Bold) == FontStyle.Bold;
Run Code Online (Sandbox Code Playgroud)

例如,类Bold上的属性使用与上面几乎相同的代码Font检查是否FontStyle.Bold是您给它的样式的一部分:

public bool Bold
{
    get
    {
        return ((this.Style & FontStyle.Bold) != FontStyle.Regular);
    }
}
Run Code Online (Sandbox Code Playgroud)