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

前端 未结 7 882

For example I have

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

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


        
7条回答
  •  忘掉有多难
    2020-12-09 15:18

    I'm using C# 8. A clean way for a callback function.

    public static void CallBackTest( Action<(string Name, int Age)> fn)
    {
        fn(("MhamzaRajput", 23));
    }
    
    public static void Main()
    {
        CallBackTest((t) =>
        {
            var ( Name, Age ) = t;
    
            Console.WriteLine(t.Name);
            Console.WriteLine(t.Age);
        });
    }
    

    Output

    MhamzaRajput
    23
    

提交回复
热议问题