将超过7的元组添加到List

时间:2017-06-01 22:35:11

标签: c#

我有这个清单:

public List<Tuple<string, string, string, string, string, string, string, Tuple<string, string, string, string, string, string, string, Tuple<string, string,string,string>>>> Load = new List<Tuple<string, string, string, string, string, string, string, Tuple<string, string, string, string, string, string, string, Tuple<string, string,string,string>>>>();

我试图在这个列表中添加值我目前已尝试过多种方式:

Load.Add(new Tuple<string, string, string, string, string, string, string, Tuple<string, string, string, string, string, string, string, Tuple<string, string, string, string>>>("1","2","3","4","5","6","7",Tuple.Create("8","9","10","11","12","13","14","15"),Tuple.Create("16","17","18","19","20")));

这对我来说真的不起作用,我做错了什么?

谢谢,

1 个答案:

答案 0 :(得分:1)

所以C#中的元组实际上是一个穷人的类,并且是制作不可读代码的好工具。当你不能写一个更明确的结构时,应该使用...即很少。如果您使用超过两个或三个项目元组,您的代码将很快变得难以理解。

您存储的所有字符串都必须具有一定的含义。您是否真的想按照评论中的建议回顾myTuple.Item8.Item8.Item3?你的元组中的每个项目是什么?

他们只是一堆相关的字符串吗?在这种情况下,使用ICollection<string> ... List<string>可能适合此处。

每个项目都有特定的角色吗?在这种情况下,一个类更合适:

public class MyThing
{
    public string Make{get;set;}
    public string Model{get;set;}
    public string Size{get;set;}
    //...etc
}

现在创建List<MyThing>并向其添加MyThing的实例:

myList.Add(new MyThing{
    Make = "Volvo",
    Model = "940",
    Size = "Massive"
})

现在,只需一眼就能理解代码的意图。