Reputation: 1465
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>'
Upvotes: 1
Views: 530
Reputation: 19545
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>>>
.
Upvotes: 2
Reputation: 2340
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
Upvotes: 1