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

前端 未结 7 857

For example I have

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

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


        
7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-09 15:20

    You can shorten it to:

    void test( Action> fn)
    {
        fn(("hello", 10));
    }
    
    test(((string s, int i) t) =>
    {
        Console.WriteLine(t.s);
        Console.WriteLine(t.i);
    });
    

    Hopefully, one day we might be able to splat the parameters from a tuple to the method invocation:

    void test(Action> fn)
    {
        fn(@("hello", 10)); // <-- made up syntax
    }
    
    test((s, i) =>
    {
        Console.WriteLine(s);
        Console.WriteLine(i);
    });
    

    But not at the moment.

提交回复
热议问题