Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create ternary operators in C#?

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

How?

like image 524
Scott S Avatar asked Feb 05 '10 07:02

Scott S


People also ask

Can I use ternary operator in C?

We use the ternary operator in C to run one code when the condition is true and another code when the condition is false. For example, (age >= 18) ? printf("Can Vote") : printf("Cannot Vote");

How do you make a ternary operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

How many ternary operators does C have?

A ternary operator takes three major arguments: The comparison argument. The result upon a false comparison. The result upon a true comparison.

Is it good practice to use ternary operator?

It's a good practice to use the ternary operator when it makes the code easier to read. If the logic contains many if...else statements, you should avoid using the ternary operators.


2 Answers

Don't believe the haters ;)

You can do this in C#. Here's an example implementation — I based the chaining off the way that Icon does theirs... if a comparison succeeds, the result is that of the right parameter, otherwise a special "failed" result is returned.

The only extra syntax you need to use is the call to Chain() after the first item.

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);
    }
}
like image 185
porges Avatar answered Sep 20 '22 12:09

porges


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

You could use extension methods to enable a fluent syntax like

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

Or, if you were being extremely clever, you could do:

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

doing so is tricky, but possible. (Hint: define a custom object that IsLessThan returns that tracks its bounds and understands how it is combined with other instances of the object. Essentially this is how LINQ-to-SQL works with regard to combining Where, Select, and so on.)

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

If you are interested in languages where you can define your own operators, you might consider looking into F#.

like image 28
Eric Lippert Avatar answered Sep 22 '22 12:09

Eric Lippert