在C#中使用列表列表作为类

Working with a List of Lists as a Class in C#
2020-10-22
  •  译文(汉语)
  •  原文(英语)

我创建了以下类来组织一些需要保存到文本文件中的变量:

public class ListOfVariablesToSave : List<List<Tuple<string, int, string>>>
{
    public List<Tuple<string, int, string>> control { get; set;}
    public List<Tuple<string, int, string>> interaction { get; set;}

    public ListOfVariablesToSave()
    {
        control = new List<Tuple<string, int, string>>
        {
            Tuple.Create("/YD/YDC/MCSTEP", 0, ""),
            Tuple.Create("/YD/YDC/NCSTEP", 0, ""),
            Tuple.Create("/YD/YDC/DCSTEC", 0, ""),
            Tuple.Create("/YD/YDC/DCTIME", 0, ""),
            Tuple.Create("/YD/YDC/ICOUTF", 0, ""),
            Tuple.Create("/YD/YDC/ICSHTF", 0, "")
        };

        interaction = new List<Tuple<string, int, string>>
        {
            Tuple.Create("/YD/YDI/MICOUP", 0, ""),
            Tuple.Create("/YD/YDI/NICOUP", 0, ""),
            Tuple.Create("/YD/YDI/DIEZON", 0, ""),
            Tuple.Create("/YD/YDI/I1IECN", 1, ""),
            Tuple.Create("/YD/YDI/I1IECT", 1, ""),
            Tuple.Create("/YD/YDI/MISTATE", 0, "")
        };
    }
}

但是,当我使用以下方法创建实例时:

ListOfVariablesToSave myListOfVariablesToSave = 
    new ListOfVariablesToSave();

我得到没有元素的列表(count = 0),尤其是当我尝试使用以下代码访问列表时:

foreach (List<Tuple<string, int, string>> DB in myListOfVariablesToSave)
{
    foreach (Tuple<string, int, string> varToSave in DB)
    {
        // DO SOMETHING.
    }
}

因为(外部)列表为空,所以没有任何反应.我想念什么?如何访问外部列表中的列表?

解决过程1

这是因为您继承了列表,但是没有在构造函数中添加任何内容.如果您希望创建的两个项目可用,请将这两行添加到构造函数的末尾:

Add(control);
Add(interaction);
速聊1:
谢谢.是的,这就是问题所在.

I created the following class to organize some variables that I need to save to a text file:

public class ListOfVariablesToSave : List<List<Tuple<string, int, string>>>
{
    public List<Tuple<string, int, string>> control { get; set;}
    public List<Tuple<string, int, string>> interaction { get; set;}

    public ListOfVariablesToSave()
    {
        control = new List<Tuple<string, int, string>>
        {
            Tuple.Create("/YD/YDC/MCSTEP", 0, ""),
            Tuple.Create("/YD/YDC/NCSTEP", 0, ""),
            Tuple.Create("/YD/YDC/DCSTEC", 0, ""),
            Tuple.Create("/YD/YDC/DCTIME", 0, ""),
            Tuple.Create("/YD/YDC/ICOUTF", 0, ""),
            Tuple.Create("/YD/YDC/ICSHTF", 0, "")
        };

        interaction = new List<Tuple<string, int, string>>
        {
            Tuple.Create("/YD/YDI/MICOUP", 0, ""),
            Tuple.Create("/YD/YDI/NICOUP", 0, ""),
            Tuple.Create("/YD/YDI/DIEZON", 0, ""),
            Tuple.Create("/YD/YDI/I1IECN", 1, ""),
            Tuple.Create("/YD/YDI/I1IECT", 1, ""),
            Tuple.Create("/YD/YDI/MISTATE", 0, "")
        };
    }
}

However, when I create an instance with:

ListOfVariablesToSave myListOfVariablesToSave = 
    new ListOfVariablesToSave();

I get the list with no elements (count = 0).In particular, when I try to access the list with the following code:

foreach (List<Tuple<string, int, string>> DB in myListOfVariablesToSave)
{
    foreach (Tuple<string, int, string> varToSave in DB)
    {
        // DO SOMETHING.
    }
}

nothing happens because the (outer) list is empty. What am I missing? How do I access the lists inside my outer list?

Solutions1

This is because you inherited the list, but you did not put anything into it in the constructor. If you would like the two items that you created to become available, add these two lines to the end of your constructor:

Add(control);
Add(interaction);
Talk1:
Thanks. Yes that was the problem.
转载于:https://stackoverflow.com/questions/30552829/working-with-a-list-of-lists-as-a-class-in-c-sharp

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入