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>'
Answer
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