"I" 除非整数和非布尔类型之外的运算符

"I" Operator except non integral and not bool types

我想了解在哪里使用 | 运算符。在 msdn 上被告知

Binary | operators are predefined for the integral types and bool.

User-defined types can overload the | operator

所以,当我看到类似这样的内容时: BindingFlags.CreateInstance | BindingFlags.ExactBinding

 NotifyFilters.Attributes | NotifyFilters.FileName | NotifyFilters.LastAccess

我假设这是重载的 | 运算符,逻辑会像:某物或其他东西等等。但是我如何指示运算符 | 是为某些 [=28= 定义的]?

这意味着枚举值是 "combined" 和 or 运算符。想象一下:

public enum MY_ENUM
    : int
{
    FOO = 1,
    BAR = 2,
    TEST = 4,
    ALL_VALUES = 7,
}

FOO | BAR | TEST 将等于 ALL_VALUES,如以下计算所示:

result := FOO | BAR | TEST
        =  1  |  2  |  4    // <-- this is a normal OR-operation
        = 7

  --> result == ALL_VALUES

结论: 枚举的所有值都像 "normal" 整数一样处理,并且可以与任何二进制或数学运算符一起使用。


See this msdn article


编辑: 如果您想更好地输出枚举变量的二元运算,您也可以在枚举上使用 [Flags]-Attribute。

不,这些 enum 在内部表示为 int。 (您可以毫无问题地将这些 enum 转换为 int,反之亦然。)这样的 ints/enums 称为标志。在 C 等其他语言中,没有额外的 enum 类型,因此它们只是普通的 ints 或其他整数类型。

所以还是如你第一句话所说的那样:

Binary | operators are predefined for the integral types and bool.

关于你的另一个问题

But how can I indicate that operator | is defined for some class?

运算符使用 operator 关键字重载。注意:并非每个运算符都是可重载的,请参阅 here.

// example addition operator from a complex number class
public static Complex operator +(Complex c1, Complex c2)
{
    Return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}

您可以使用 Reflection to determine at runtime whether a class implements a certain operator. Internally, operators are just methods. For example, the == operator is implemented in the method op_Equality. So, if such a method exists, the == operator is available. (You only can see those methods with reflection, they are not callable in normal C# code). See GetMethod() 中的反射 API 如何确定运算符的存在。

+ 运算符的代码示例

MethodInfo operator = typeof(YourClass).GetMethod("op_Addition");

if (operator != null) // if null the operator is not implemented
   operator.Invoke(null, firstInstance, secondInstance);

我不知道 | 运算符的方法名称是什么,但您可以使用反编译工具轻松检查它。它应该类似于 op_BinaryOr.

如果您只是想知道您是否可以在代码中使用运算符,这应该由您的 IDE 提供,Visual Studio 例如在 IntelliSense 自动完成列表或对象中显示运算符浏览器。

是的。这样做没有多大意义,但您绝对可以覆盖 |运算符:

public static MyClass operator |(MyClass left, MyClass right)
{
    //your logic here
}

基本上,该运算符的作用是将两个整数的十六进制值组合在一起,如下所示:

int a = 0x0402;  //1026
int b = 0x5030;  //20528
int c = a | b;   //21554 (same as a+b)
string hex = "0x"+ c.ToString("X4"); //0x5432

但不要将它与 + 运算符混淆

int a = 0x0007;  //7
int b = 0x000B;  //11
int c = a | b;   //15 (a+b would be 0x0012)
string hex = "0x"+ c.ToString("X4"); //0x000F

对于枚举,基础值是什么 | -d
这是一个例子

        [Flags]
        enum ActionFlags : int
        {
            DoThis = 0x00000001,
            DoThat = 0x00000010,
            DoOtherThing = 0x00000100,
            DoAnotherThing = 0x00001000,
            MaxValue = 0x00001111,
            MinValue = 0x00000000,
        }

        void DoStuff(ActionFlags what_to_do)
        {
            if ((int)(what_to_do) > (int)(ActionFlags.MaxValue) || 
                (int)(what_to_do) < (int)(ActionFlags.MinValue))
                throw new ArgumentException();
            if(what_to_do.HasFlag(ActionFlags.DoThis))
            {
                // do this
            }
            if (what_to_do.HasFlag(ActionFlags.DoThat))
            {
                // do that
            }
            if (what_to_do.HasFlag(ActionFlags.DoOtherThing))
            {
                // do other thing
            }
            if (what_to_do.HasFlag(ActionFlags.DoAnotherThing))
            {
                // do another thing
            }
        }


DoStuff(ActionFlags.DoThat | ActionFlags.DoOtherThing);

你不应该自己制作的原因 | , 就像 msdn 所说的那样,它已经为所有数字类型预定义了,对于大多数其他 类 这种二进制逻辑是不必要的。