是否可以在 C# 中创建一个新的运算符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1040114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to create a new operator in c#?
提问by Aaron Palmer
I know you can overload an existing operator. I want to know if it is possible to create a new operator. Here's my scenario.
我知道您可以重载现有的运算符。我想知道是否可以创建一个新的运算符。这是我的场景。
I want this:
我要这个:
var x = (y < z) ? y : z;
To be equivalent to this:
相当于:
var x = y <? z;
In other words, I would like to create my own <?
operator.
换句话说,我想创建自己的<?
运算符。
采纳答案by AgileJon
No, it is not possible. You would need to create a method instead
不,这是不可能的。您需要创建一个方法
回答by Dario
No, but you can overload some existing operatorsin C#.
不,但您可以在 C# 中重载一些现有的运算符。
In some other languages, like F#, you can use:
在其他一些语言(如 F#)中,您可以使用:
let (<?) = min
回答by David Z
As the other answers have said, you can't create a new operator - at least, not without altering the lexer and parser that are built into the compiler. Basically, the compiler is built to recognize that an individual character like <
or ?
, or a pair like >>
or <=
, is an operator and to treat it specially; it knows that i<5
is an expression rather than a variable name, for instance. Recognizing an operator as an operator is a separate process from deciding what the operator actually does, and is much more tightly integrated into the compiler - which is why you can customize the latter but not the former.
正如其他答案所说,你不能创建一个新的运算符 - 至少,在不改变编译器内置的词法分析器和解析器的情况下不能。基本上,编译器被构建为识别像<
or?
或一对像>>
or这样的单个字符<=
是一个运算符并对其进行特殊处理;例如,它知道这i<5
是一个表达式而不是变量名。将操作符识别为操作符与决定操作符实际做什么是一个独立的过程,并且更紧密地集成到编译器中 - 这就是为什么您可以自定义后者而不是前者。
For languages which have an open-source compiler (such as GCC) you could, theoretically, modify the compiler to recognize a new operator. But it wouldn't be particularly easy, and besides, everyone would need your custom compiler to use your code.
对于具有开源编译器(例如 GCC)的语言,理论上您可以修改编译器以识别新的运算符。但这不会特别容易,此外,每个人都需要您的自定义编译器来使用您的代码。
回答by Paul Hooper
Not only can you not do that, but why would you want to?
你不仅不能这样做,而且你为什么要这样做?
I'm not sure what type your y and z are, but if they are of a numeric value type, you could probably use:
我不确定你的 y 和 z 是什么类型,但如果它们是数值类型,你可能会使用:
var x = Math.Min(y, z);
Though personally, I would still prefer:
虽然就个人而言,我仍然更喜欢:
var x = (y < z) ? y : z;
But I'm a bit of a ? : junky.
但我有点?: 垃圾。
Good code is not just tight and efficient, but also readable. Even if you are the only one ever reading it, you'd come back to that <?
operator one day and wonder what the heck that did.
好的代码不仅要紧凑高效,还要具有可读性。即使你是唯一一个读过它的人,<?
有一天你会回到那个操作员那里,想知道它到底做了什么。
回答by Rony
No, but you can create Extension Methods instead of this
不,但您可以创建扩展方法而不是这个
y.MethodName(z)
回答by Tiago Peczenyj
you can try to overload other operator like %
or +
to act as a <?
operator.
您可以尝试重载其他运算符%
或+
充当<?
运算符。
Will be fun
会很有趣
回答by Jeff
I'm surprised no one mentioned "order of operations"
.
我很惊讶没有人提到"order of operations"
。
When the compiler evaluates an expression it has to be concerned with performing the operations in the correct order so that (1+2*3) = (2*3+1)
multiplication always happens before addition at the same "level"
in the expression.
当编译器对表达式求值时,它必须关注以正确的顺序执行运算,以便在表达式中(1+2*3) = (2*3+1)
乘法总是先于加法"level"
。
When you override and operator, you can change what the operator does, but not the order in which the compiler will evaluate it. If you did created a new operator, there is no way to tell the compiler what order to evaluate it in relation to the others. So if you write x <? 2 + 5
Do you perform the x <? 2
first then add 5 or do you perform the addition first and then do x <? 7
.
当您覆盖 and 运算符时,您可以更改运算符的作用,但不能更改编译器对其求值的顺序。如果您确实创建了一个新运算符,则无法告诉编译器以什么顺序对其进行评估。因此,如果您编写x <? 2 + 5
是x <? 2
先执行第一个然后添加 5 还是先执行添加然后执行x <? 7
.