Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define new operators in C#? [duplicate]

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

I love C#, but one thing I wish it had was the ability to define my own operators on classes, like A => B instead of having to do A.Implies(B). I think it would be really cool if you could assign an identifier of any length in a set like [+-*/&^|%$#@><]+ to a class method and then use it like an operator. Does anybody know if there's some sort of extension, or if it's even possible to make one, that does this?

like image 229
Chris Avatar asked Feb 20 '10 21:02

Chris


2 Answers

This is not built into the language. You are stuck with overloading the predefined operators that come with the language (overloadable operators).

If you like that style of debugging you might want to consider F#. It is a functional language that runs on top the .NET framework and gives you the ability to define any operator you want.

like image 72
smaclell Avatar answered Oct 10 '22 06:10

smaclell


The list of operators that can be overloaded is here.

Operator overloading only lets you change the behavior of operators, not define new ones.

And although A => B as A.Implies(B) might sound cool, it will cause you problems later (in 6 months, when you're trying to figure out what your code is supposed to do).

like image 25
Seth Avatar answered Oct 10 '22 06:10

Seth