Home > OS >  Up to several variables can be used for tuples?
Up to several variables can be used for tuples?

Time:08-20

I have heard that up to 7 variables can be used, but I have requested more. I want to know the maximum number of variables that can be used.

CodePudding user response:

It is effectively unlimited. The documentation for a Tuple with more than seven properties is here. The eighth property can itself be a Tuple. The eighth property of that can also be a Tuple and so on until the system runs out of space. As an example, this code:

var data =
    new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int, int, int, int, int, Tuple<int, int>>>(
        1, 2, 3, 4, 5, 6, 7,
        new Tuple<int, int, int, int, int, int, int, Tuple<int, int>>(
            1, 2, 3, 4, 5, 6, 7,
            Tuple.Create(1, 2)));

Console.WriteLine(data.ToString());

produces the following output:

(1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 1, 2)

It's worth noting that this code:

var data = Tuple.Create(
    1, 2, 3, 4, 5, 6, 7,
    Tuple.Create(
        1, 2, 3, 4, 5, 6, 7,
        Tuple.Create(1, 2)));

Console.WriteLine(data.ToString());

produces this slightly different output:

(1, 2, 3, 4, 5, 6, 7, (1, 2, 3, 4, 5, 6, 7, (1, 2)))

The documentation does say this:

To create an n-tuple with nine or more components, you must call the Tuple<T1,T2,T3,T4,T5,T6,T7,TRest> constructor. The static factory methods of the Tuple class do not support the creation of Tuple objects with more than eight components.

  • Related