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

前端 未结 7 856

For example I have

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

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


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

    The closest that I could get.

    public static class DeconstructExtensions
    {
        public static Action Deconstruct(this Action<(T1, T2)> action) => (a, b) => action((a, b));
        public static Action<(T1, T2)> Construct(this Action action) => a => action(a.Item1, a.Item2);
    }
    
    class Test
    {
        private void fn((string, int) value) { }
    
        private void test(Action> fn)
        {
            fn(("hello", 10));
        }
    
        private void Main()
        {
            var action = new Action((s, i) =>
            {
                Console.WriteLine(s);
                Console.WriteLine(i);
            });
    
            test(action.Construct());
        }
    }
    

提交回复
热议问题