In C# 7 is it possible to deconstruct tuples as method arguments

前端 未结 7 820

For example I have

private void test(Action> fn)
{
    fn((\"hello\", 10));
}

test(t => 
 {
    var (s, i) = t;
    C         


        
相关标签:
7条回答
  • 2020-12-09 15:32

    Here are more concise syntax variations that don't require any extra imports. No, it does not resolve wishes for "splatter" syntax discussed in comments, but no other answer used the ValueTuple syntax for the initial parameter definition.

    void test(Action<(string, int)> fn)
    {
        fn(("hello", 10));
    }
    
    // OR using optional named ValueTuple arguments
    void test(Action<(string word, int num)> fn)
    {
        fn((word: "hello", num: 10));
    }
    

    The invocation using a lambda expression is not that verbose, and the ValueTuple components can still be retrieved using minimal syntax:

    test( ((string, int) t) => {
        var (s, i) = t;
    
        Console.WriteLine(s);
        Console.WriteLine(i);
    });
    
    0 讨论(0)
提交回复
热议问题