Is there idiomatic C# equivalent to C's comma operator?

前端 未结 7 1964
死守一世寂寞
死守一世寂寞 2020-12-14 07:18

I\'m using some functional stuff in C# and keep getting stuck on the fact that List.Add doesn\'t return the updated list.

In general, I\'d like to call

7条回答
  •  暖寄归人
    2020-12-14 07:59

    I know that this thread is very old, but I want to append the following information for future users:

    There isn't currently such an operator. During the C# 6 development cycle a semicolon operator was added, as:

    int square = (int x = int.Parse(Console.ReadLine()); Console.WriteLine(x - 2); x * x);
    

    which can be translated as follows:

    int square = compiler_generated_Function();
    
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private int compiler_generated_Function()
    {
        int x = int.Parse(Console.ReadLine());
    
        Console.WriteLine(x - 2);
    
        return x * x;
    }
    

    However, this feature was dropped before the final C# release.

提交回复
热议问题