简体   繁体   English

我可以在C#中创建三元运算符吗?

[英]Can I create ternary operators in C#?

I want to create a ternary operator for a < b < c which is a < b && b < c. 我想为<b <c创建一个三元运算符,它是<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. 或者你可以想到的任何其他选项<b> c等等......我是自己的短片形式的粉丝,我想创建它,因为我在高中学习编程。

How? 怎么样?

Don't believe the haters ;) 不要相信仇敌;)

You can do this in C#. 可以用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. 这是一个示例实现 - 我基于Icon的方式进行链接...如果比较成功,结果是正确参数的结果,否则返回特殊的“失败”结果。

The only extra syntax you need to use is the call to Chain() after the first item. 您需要使用的唯一额外语法是在第一个项目之后调用Chain()

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);
    }
}

Sorry, you cannot create your own operators in C#. 抱歉,您无法在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.) (提示:定义IsLessThan返回的自定义对象,它跟踪其边界并理解它与对象的其他实例的组合方式。本质上,这就是LINQ-to-SQL在组合Where,Select等方面的工作方式。)

But you cannot define your own operator syntaxes in C#. 但是您无法在C#中定义自己的运算符语法。

If you are interested in languages where you can define your own operators, you might consider looking into F#. 如果您对可以定义自己的运算符的语言感兴趣,可以考虑查看F#。

You can't do that. 你不能这样做。 You can only implement existing operators, and there is no ternary .<.<. 你只能实现现有的运营商,而且没有三元。<。<。 operator in C#. C#中的运算符。

Besides, such an operator would be ambiguous with existing comparing operators. 此外,这样的运算符与现有的比较运算符不一致。 For example, would the expression a == b == c == d mean ((a == b) == c) == d or (a == b == c) == d ? 例如,表达式a == b == c == d意味着((a == b) == c) == d(a == b == c) == d

您是否尝试在Google中搜索三元运算符以查看是否已存在某些内容?

No. Not in C#. 不,不是在C#中。 Language extension in this way is not available in any current version of C#, but Luca Bolognese pointed to using the compiler as a service and some functionality which could permit extending the language in such a way here: http://microsoftpdc.com/Sessions/FT11 . 这种方式的语言扩展在任何当前版本的C#中都不可用,但是Luca Bolognese指出使用编译器作为服务和一些功能可以允许在这里扩展语言: http//microsoftpdc.com/Sessions / FT11

If you want to do that for the primitive datatypes, you're out of luck. 如果你想对原始数据类型那样做,那你就不走运了。 C# doesn't support adding operators to those. C#不支持向其添加运算符。

In your own datatypes it is possible to return a special datatype which stores the intermediate result and the comparisson result. 在您自己的数据类型中,可以返回一个特殊的数据类型,该数据类型存储中间结果和比较结果。 However, I suggest you just stick to the c# language - if you really want the a < b < c operator style, switch to Python. 但是,我建议您坚持使用c#语言 - 如果您真的想要a < b < c运算符样式,请切换到Python。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM