2

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

I was wondering if it is at all possible to create custom operators such as if (a atleast 5) in C#.

The above statement would read, if a is at least 5 then do this.

I am looking for more of a keyword type operator like typeof or is.

Community
  • 1
  • 1
Kyle Uithoven
  • 2,414
  • 5
  • 30
  • 43

1 Answers1

5

No, C# does not allow custom operators. You can overload certain pre-existing ones, but you cannot create your own like in Haskell or F#.

Gabe
  • 84,912
  • 12
  • 139
  • 238
  • If using Visual Studio is there a way to do this with macros? or any type of environment add-ons? – Kyle Uithoven May 24 '11 at 16:12
  • 1
    @Kyle Uithoven: no. How would that possibly work? If it were a VS macro, it would have to put in regular C# code with function calls. You can't get around the fact that C# does not let you define custom operators (and thank $DEITY it doesn't). – siride May 24 '11 at 16:14
  • @Kyle c# doesn't support macros either (by design, and for good reasons). Of course, add enough upstream custom tooling and you can probably do anything you want: I just wouldn't. – Marc Gravell May 24 '11 at 16:15
  • 5
    @Kyle: No, there is no way to modify C# like that. You might consider looking at extension methods, though, to allow you to type `if (a.atleast(5))` – Gabe May 24 '11 at 16:16
  • 1
    programmers are supposed to be open minded :P come on what's the harm?! I actually totally disagree with peeps who don't want a custom operator here. If an operator being customized allows a dev team to customize their app domain's programming techniques then do it! I think as nerds we get too caught up in trying to look smart with our code (definitely guilty myself), when instead we need to look as newb as possible at least on the surface level. This allows newer people to not be intimidated and more intra-dev trust! trust = good business!! :D – Michael Socha Mar 05 '15 at 21:39