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

27,510

Solution 1

No, it is not possible. You would need to create a method instead

Solution 2

No, but you can overload some existing operators in C#.

In some other languages, like F#, you can use:

let (<?) = min

Solution 3

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.

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.

Solution 4

I'm surprised no one mentioned "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.

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.

Solution 5

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.

Share:
27,510
Aaron Palmer
Author by

Aaron Palmer

Hubby. Daddy. Ninja. Developer.

Updated on July 27, 2020

Comments

  • Aaron Palmer
    Aaron Palmer almost 4 years

    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.

  • Camilo Martin
    Camilo Martin over 13 years
    Not if it's a known feature in another language like this question asks: stackoverflow.com/questions/523403/…
  • Camilo Martin
    Camilo Martin over 13 years
    Just for the sake of clarity, in C++ people use macros to do things like this? I don't know C++, just asking.
  • JNF
    JNF almost 11 years
    Same can be said for any operator overloading
  • עדי ביננבאום
    עדי ביננבאום almost 11 years
    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.
  • Electric Coffee
    Electric Coffee about 10 years
    or just use languages where operators are infix methods, like scala or f#
  • Piotr Kula
    Piotr Kula almost 10 years
    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.
  • Thomas Levesque
    Thomas Levesque almost 10 years
    Actually, this should be min, not max ;)
  • Patrick Hofman
    Patrick Hofman almost 10 years
    This is not an answer to his question.
  • Oleksandr Zolotarov
    Oleksandr Zolotarov almost 10 years
    2.IfNotSmallEnoughThen(1) :)
  • SparK
    SparK almost 8 years
    I have true.Xor(false) here... :(
  • Marcelo Scofano Diniz
    Marcelo Scofano Diniz almost 4 years
    No, it is not an answer, but it somehow illustrates how absurd it may evolve...