Do nothing when “other side” of ternary operator is reached?

后端 未结 5 1770
醉酒成梦
醉酒成梦 2021-01-12 01:43

Note: I\'ve seen this question asked sometimes before (a, b, c), but neither of these was in C#, nor helpful.

Assume I\'m using the ? : ternary

5条回答
  •  借酒劲吻你
    2021-01-12 02:26

    You can now achieve this using the Discard operator, if you really want to.

    public class Program
    {
        public static void Main()
        {
            int r=5;
            _ = r==5 ? r=0 : 0;
            Console.WriteLine($"{r}");
            // outputs 0
        }
    }
    

    You can now also do

    _=Foo() ? Bar() : Baz();
    

    As long as Bar and Baz return the same or convertible type.

提交回复
热议问题