Practical example where Tuple can be used in .Net 4.0?

后端 未结 19 652
后悔当初
后悔当初 2020-12-04 07:44

I have seen the Tuple introduced in .Net 4 but I am not able to imagine where it can be used. We can always make a Custom class or Struct.

19条回答
  •  有刺的猬
    2020-12-04 08:07

    Well in my case, I had to use a Tuple when I found out that we cannot use out parameter in an asynchronous method. Read about it here. I also needed a different return type. So I used a Tuple instead as my return type and marked the method as async.

    Sample code below.

    ...
    ...
    // calling code.
    var userDetails = await GetUserDetails(userId);
    Console.WriteLine("Username : {0}", userDetails.Item1);
    Console.WriteLine("User Region Id : {0}", userDetails.Item2);
    ...
    ...
    
    private async Tuple GetUserDetails(int userId)
    {
        return new Tuple("Amogh",105);
        // Note that I can also use the existing helper method (Tuple.Create).
    }
    

    Read more about Tuple here. Hope this helps.

提交回复
热议问题