Method Names for Operator Methods in C#
-
19-08-2019 - |
Question
Does anyone have a exhaustive list of the names that C#/CLR gives to operators? (Maybe my lack of sleep is kicking in, but I can't seem to find it on Google) E.g. 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.
Solution
Here is the full list of C# overloadable operators
You can find a list of the operator Metadata/Generated MSIL names under Framework Design Guidelines -> Operator Overloads.
There is a different F# operator overload list.
Finally, refer to ECMA-335 Common Language Infrastructure (CLI) I.10.3 Operator overloading, where the operators for C++/CLI are listed.
OTHER TIPS
Based on the Expression class:
== 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
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.
According to a referenced StackOverflow question, the following method names are also emitted/understood by the C# compiler, but
- I have run tests in Visual Studio 2015 and C#6.0 attempting to get these to work, and
- 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.
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