Can I create ternary operators in C #?

I want to create a ternary operator for <b <c, which is <b && b <c. or any other option that you can think of that <b> c and so on ... I am a fan of my own short form, and I wanted to create it since I studied programming in high school.

How?

+6
c # ternary-operator
source share
6 answers

Do not believe the haters;)

You can do it in C #. Here is an implementation example - I based the chain on the fact that Icon makes them ... if the comparison succeeds, the result is the correct parameter, otherwise a special "unsuccessful" result is returned.

The only additional syntax you need to use is calling Chain() after the first element.

 class Program { static void Main(string[] args) { if (2.Chain() < 3 < 4) { Console.WriteLine("Yay!"); } } } public class Chainable<T> where T : IComparable<T> { public Chainable(T value) { Value = value; Failed = false; } public Chainable() { Failed = true; } public readonly T Value; public readonly bool Failed; public static Chainable<T> Fail { get { return new Chainable<T>(); } } public static Chainable<T> operator <(Chainable<T> me, T other) { if (me.Failed) return Fail; return me.Value.CompareTo(other) == -1 ? new Chainable<T>(other) : Fail; } public static Chainable<T> operator >(Chainable<T> me, T other) { if (me.Failed) return Fail; return me.Value.CompareTo(other) == 1 ? new Chainable<T>(other) : Fail; } public static Chainable<T> operator ==(Chainable<T> me, T other) { if (me.Failed) return Fail; return me.Value.CompareTo(other) == 0 ? new Chainable<T>(other) : Fail; } public static Chainable<T> operator !=(Chainable<T> me, T other) { if (me.Failed) return Fail; return me.Value.CompareTo(other) != 0 ? new Chainable<T>(other) : Fail; } public static bool operator true(Chainable<T> me) { return !me.Failed; } public static bool operator false(Chainable<T> me) { return me.Failed; } public override bool Equals(object obj) { return Value.Equals(obj); } public override int GetHashCode() { return Value.GetHashCode(); } } public static class ChainExt { public static Chainable<T> Chain<T>(this T value) where T : IComparable<T> { return new Chainable<T>(value); } } 
+10
source share

Sorry, you cannot create your own statements in C #.

You can use extension methods to enable free syntax like

 bool f = b.IsBetween(a, c); 

Or, if you were very smart, you could do:

 bool f = a.IsLessThan(b).IsLessThan(c); 

it is difficult to do, but possible. (Hint: defining a custom object that IsLessThan returns, tracks its boundaries and understands how it blends with other instances of the object. Essentially, this is how LINQ-to-SQL works with respect to joining Where, Select, etc.) .

But you cannot define your own operator syntaxes in C #.

If you are interested in languages โ€‹โ€‹in which you can define your own operators, you can consider searching in F #.

+4
source share

You cannot do this. You can only implement existing operators, and there is no triple. <. operator in c #.

In addition, such an operator will be ambiguous with existing comparison operators. For example, the expression a == b == c == d means ((a == b) == c) == d or (a == b == c) == d ?

+3
source share

Have you tried to find three-dimensional operators in google to find out if something already exists?

0
source share

Not. Not in C #. An extension of the language is thus not available in any current version of C #, but Luca Bolognese pointed out the use of the compiler as a service and some functionality that would allow the extension of the language in this way: http://microsoftpdc.com/Sessions/FT11 .

0
source share

If you want to do this for primitive data types, you're out of luck. C # does not support adding statements to them.

In your own data types, you can return a special data type that stores the intermediate result and the comparison result. However, I suggest you just stick with C # - if you really need the style of the a < b < c operator, switch to Python.

0
source share

All Articles