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

后端 未结 19 655
后悔当初
后悔当初 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:21

    I used a tuple to solve Problem 11 of Project Euler:

    class Grid
    {
        public static int[,] Cells = { { 08, 02, 22, // whole grid omitted
    
        public static IEnumerable<Tuple<int, int, int, int>> ToList()
        {
            // code converts grid to enumeration every possible set of 4 per rules
            // code omitted
        }
    }
    

    Now I can solve the whole problem with:

    class Program
    {
        static void Main(string[] args)
        {
            int product = Grid.ToList().Max(t => t.Item1 * t.Item2 * t.Item3 * t.Item4);
            Console.WriteLine("Maximum product is {0}", product);
        }
    }
    

    I could have used a custom type for this, but it would have looked exactly like Tuple.

    0 讨论(0)
提交回复
热议问题