1

I have a list of tuples and cannot add them to a list. What causes this compile error? https://dotnetfiddle.net/vhR3vP

    using System;
    using System.Collections.Generic;               
public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World");
         List<Tuple<string,string,string,string,string,string,string,string>> ExecutionsDetailsTuple =new  List<Tuple<string,string,string,string,string,string,string,string>>(); // format executionid, symbol, quantity, price, tradedirection, orderaction, account, executiontime
        var person= Tuple.Create<string,string,string,string,string,string,string,string>("1","2","3","4","5","6","7","8");
        ExecutionsDetailsTuple.Add(person);
        
    }
}

compile error:

Compilation error (line 10, col 3): The best overloaded method match for 'System.Collections.Generic.List<System.Tuple<string,string,string,string,string,string,string,string>>.Add(System.Tuple<string,string,string,string,string,string,string,string>)' has some invalid arguments
Compilation error (line 10, col 30): Argument 1: cannot convert from 'System.Tuple<string,string,string,string,string,string,string,System.Tuple<string>>' to 'System.Tuple<string,string,string,string,string,string,string,string>'

2 Answers 2

2

The return type of Tuple.Create<> with eight arguments is defined as:

Tuple<T1,T2,T3,T4,T5,T6,T7,Tuple<T8>>

Notice the Tuple<T8> at the end. You think you would create a Tuple<T1,T2,T3,T4,T5,T6,T7,T8> instance with the Tuple.Create() method, but you are not.

As the number of generic parameters for the Tuple class is limited to eight, but there might be a requirement for more than eight values, the class is designed that way, that the 8th generic parameter must be a Tuple type. So the general structure will be: seven values plus an additional Tuple for the remaining values. Assuming you want to create a tuple of 25 values, you would build a chain of four Tuple instances (7+7+7+4).

When you want to use a tuple of eight values you have to change the type of your variable to List<Tuple<string,string,string,string,string,string,string,Tuple<string>>>.

Sign up to request clarification or add additional context in comments.

2 Comments

i tried it but i have this error. how do i fix it if i have to have tuple as last row? dotnetfiddle.net/WSa0bl
@junkone There is no overload of Tuple.Create() which takes nine arguments. You have to use the Tuple constructor to create a Tuple with eight values, where the 8th argument is a Tuple of two values. This will result in a tuple with 7+2 values.
1

Tuples should not be used in "production code" unless you deal with CLI or some other you get your input from some other language.

Because of Generics limitations in C# Tuples can have at most 8 generic values. And as per documentation: The last element of an eight element Tuple must be a Tuple.

To remove this error, convert this tuple to class, or convert your tuple so it uses a Tuple as a last element

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.