简体   繁体   English

C#中操作员方法的方法名称

[英]Method Names for Operator Methods in C#

Does anyone have a exhaustive list of the names that C#/CLR gives to operators? 有没有人有一个详尽的C#/ CLR给运营商的名单? (Maybe my lack of sleep is kicking in, but I can't seem to find it on Google) Eg op_Addition, op_Subtraction. (也许我的睡眠不足,但我似乎无法在Google上找到它)例如op_Addition,op_Subtraction。 Furthermore is there any chance that these would be different in other cultures? 此外,这些在其他文化中会有什么不同吗?

I am trying to create a class that can add/subtract etc. two objects and I have done all the primitives - I just need to do the 'rest'. 我正在尝试创建一个可以添加/减去两个对象的类,我已经完成了所有原语 - 我只需要做'休息'。

Many thanks. 非常感谢。

Based on the Expression class: 基于Expression类:

== op_Equality
!= op_Inequality
>  op_GreaterThan
<  op_LessThan
>= op_GreaterThanOrEqual
<= op_LessThanOrEqual
&  op_BitwiseAnd
|  op_BitwiseOr
+  op_Addition
-  op_Subtraction
/  op_Division
%  op_Modulus
*  op_Multiply
<< op_LeftShift
>> op_RightShift
^  op_ExclusiveOr
-  op_UnaryNegation
+  op_UnaryPlus
!  op_LogicalNot
~  op_OnesComplement
   op_False
   op_True
++ op_Increment
-- op_Decrement

Here is the full list of C# overloadable operators 以下是C#可重载运算符的完整列表

You can find a list of the operator Metadata/Generated MSIL names under Framework Design Guidelines -> Operator Overloads . 您可以在框架设计指南 - >操作员重载下找到运营商元数据/生成的MSIL名称列表。

There is a different F# operator overload list . 有一个不同的F#运算符重载列表

Finally, refer to ECMA-335 Common Language Infrastructure (CLI) I.10.3 Operator overloading, where the operators for C++/CLI are listed. 最后,请参阅ECMA-335公共语言基础结构(CLI) I.10.3运算符重载,其中列出了C ++ / CLI的运算符。

The accepted answer is good because it links to a list of overloadable operators, but it doesn't strictly answer the question because it doesn't provide the names of the operators when they are compiled. 接受的答案很好,因为它链接到一个可重载的运算符列表,但它没有严格回答这个问题,因为它在编译时没有提供运算符的名称。

The runner up answer includes that, but it is an incomplete list and not well organized. 获得者的回答包括,但它是一个不完整的列表,并没有很好的组织。 For completeness' sake, I made my own list, including a few operators that you might expect to be overloadable but that aren't. 为了完整起见,我创建了自己的列表,包括一些您可能期望可以超载的运算符,但事实并非如此。 The list is in order of operator precedence, from highest (tightest) priority to lowest. 该列表按运营商优先顺序排列,从最高(最紧)优先级到最低优先级。

Outside of Order of Operations (used when necessary to coerce types)
   op_Implicit
   op_True
   op_False

Unary (only one operator)
++ op_Increment (Includes preincrement and postincrement)
-- op_Decrement (Includes predecrement and postdecrement)
-  op_UnaryNegation
+  op_UnaryPlus
!  op_LogicalNot
~  op_OnesComplement
   op_Explicit
await (Not overloadable)

Multiplicative
/  op_Division
%  op_Modulus
*  op_Multiply

Additive
+  op_Addition
-  op_Subtraction

Shift
<< op_LeftShift
>> op_RightShift

Relational
>  op_GreaterThan
<  op_LessThan
>= op_GreaterThanOrEqual
<= op_LessThanOrEqual
as (Not overloadable)
is (Not overloadable)

Equality
== op_Equality
!= op_Inequality

And
&  op_BitwiseAnd

Exclusive Or
^  op_ExclusiveOr

Inclusive Or
|  op_BitwiseOr

Conditional And
&& (Not overloadable - use "implicit operator bool" or "operator true")

Contional Or
|| (Not overloadable - use "implicit operator bool" or "operator true")

Null-Coalescing Operator
?? (Not overloadable)

Ternary
?: (Not overloadable - use "implicit operator bool" or "operator true")

Assignment
=  (Not overloadable - for combined operator and assignment (such as +=), just use the operator (+))

Note: The C# compiler prefers implicit operator bool instead of the True and False operators, if both are defined. 注意:如果定义了两者,C#编译器更喜欢implicit operator bool而不是True和False运算符。

According to a referenced StackOverflow question , the following method names are also emitted/understood by the C# compiler, but 根据引用的StackOverflow问题 ,C#编译器也会发出/理解以下方法名称,但是

  • I have run tests in Visual Studio 2015 and C#6.0 attempting to get these to work, and 我已经在Visual Studio 2015和C#6.0中运行测试,试图让它们工作,并且
  • The C#5.0 spec lists the "complete set of unary [and binary] operator function names" (Appendix A, page 469) and these appear nowhere therein. C#5.0规范列出了“完整的一元[和二元]运算符函数名称集”(附录A,第469页),其中没有出现。
Unary
&   op_AddressOf
*   op_PointerDereference

Binary
.   op_MemberSelection
->  op_PointerToMemberSelection

    op_SignedRightShift
    op_UnsignedRightShift

=   op_Assign
*=  op_MultiplicationAssignment
/=  op_DivisionAssignment
%=  op_ModulusAssignment
-=  op_SubtractionAssignment
+=  op_AdditionAssignment
<<= op_LeftShiftAssignment
>>= op_RightShiftAssignment
|=  op_ExclusiveOrAssignment
&=  op_BitwiseAndAssignment
|=  op_BitwiseOrAssignment

    op_UnsignedRightShiftAssignment
    op_SignedRightShiftAssignment

,   op_Comma

This website gives the list of names for C# operators, displaying the operator, Action (name), Example & Result: C# Operator List 该网站提供了C#操作符的名称列表,显示了操作符,操作(名称),示例和结果: C#操作员列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM