Is it possible to create a new operator in c#?

Clash Royale CLAN TAG#URR8PPPIs it possible to create a new operator in c#?
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.
<?
@JesonPark - Not true. F# has it as others have pointed out, and C++ has it as well. CoffeeScript offers several new operators as syntactic sugar for JavaScript idioms, and it's so customizable that you could describe it as allowing custom operators. The last one's a bit tricky, since you'd technically be extending the language.
– Justin Morgan
Jul 24 '13 at 18:55
@JustinMorgan: as mentioned in CodeProject article "C++ supports operator overloading, but you are not allowed to create your own operators" this is emulation!!
– AminM
Jul 24 '13 at 19:02
@JesonPark - I don't see the article you're talking about, but the example from Cogwheel's answer (on the SO question I linked to) results in code that works exactly like custom operators. Whether or not they're baked into the language, you can make them work. Either way, your statement (that no language has this capability) is invalid for F#.
– Justin Morgan
Jul 24 '13 at 20:00
@JesonPark In Scala it would be
implicit class <?(y: Int) { def <?(z: Int) = y min z } -- the implicit class to add a method to Int, and then the method itself. This particular method wouldn't work because <? is a xml-start keyword.– Daniel C. Sobral
Mar 31 '14 at 16:01
implicit class <?(y: Int) { def <?(z: Int) = y min z }
Int
<?
7 Answers
7
No, it is not possible. You would need to create a method instead
No, but you can overload some existing operators in C#.
In some other languages, like F#, you can use:
let (<?) = min
Actually, this should be min, not max ;)
– Thomas Levesque
Jul 1 '14 at 8:32
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.
<
?
>>
<=
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.
Just for the sake of clarity, in C++ people use macros to do things like this? I don't know C++, just asking.
– Camilo Martin
Nov 7 '10 at 12:27
or just use languages where operators are infix methods, like scala or f#
– Electric Coffee
Feb 20 '14 at 9:10
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:
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.
<?
Not if it's a known feature in another language like this question asks: stackoverflow.com/questions/523403/…
– Camilo Martin
Nov 7 '10 at 12:25
Same can be said for any operator overloading
– JNF
Jul 5 '13 at 8:01
I disagree. I think operator overloading makes sense in many cases. For instance, if adding two instances of a type together makes sense, you might wish to overload the + operator instead of creating an .Add() method. Intuitively, someone sees "+" and they understand the operator is arithmetically adding the two things together. If that is what the overload is effectively doing, then it's easily understood.
– Paul Hooper
Jul 15 '13 at 19:35
I would like to use
==== so that my whole dev team knows that we are using a overridden test for that specific object. Thats all, but yea.. All i wanted to avoid is using methods in if's.. just messing around.– ppumkin
Apr 29 '14 at 9:26
====
No, but you can create Extension Methods instead of this
y.MethodName(z)
2.GetMin(1) is weird
– Dario
Jun 24 '09 at 19:15
2.IfNotSmallEnoughThen(1) :)
– Alexander Zolotaryov
Jul 8 '14 at 16:56
I have true.Xor(false) here... :(
– SparK
May 31 '16 at 20:44
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 <? 2first then add 5 or do you perform the addition first and then do x <? 7.
x <? 2 + 5
x <? 2
x <? 7
you can try to overload other operator like % or + to act as a <? operator.
%
+
<?
Will be fun
This is not an answer to his question.
– Patrick Hofman
Jul 1 '14 at 8:33
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
no language have This capability...
– AminM
Jul 5 '13 at 16:05