C# 使用列表处理数据

C# 使用列表处理数据,c#,C#,这是坏习惯吗?还是完全可以这样做 private readonly List<KeyValuePair<GameType, KeyValuePair<string, string>>> _tempStats = new List<KeyValuePair<GameType, KeyValuePair<string, string>>>(); 顺便说一下,GameType是一个枚举器 我从一个表中下载了一些数据,该表有几个不

这是坏习惯吗?还是完全可以这样做

private readonly List<KeyValuePair<GameType, KeyValuePair<string, string>>> _tempStats = new List<KeyValuePair<GameType, KeyValuePair<string, string>>>();
顺便说一下,GameType是一个枚举器

我从一个表中下载了一些数据,该表有几个不同的游戏类型,其中有两个字符串与之关联。因此,它将解析数据并确定分配给它的游戏类型,然后查找表键及其值。它是有效的,它存储了信息,我可以毫无问题地检索到它,但它只是看起来有一个带有KeyValuePair的KeyValuePair列表是不正确的,但也许是这样。使用元组是更好的方法吗

我当前对列表的使用情况

    private void ParseNodeText(string nText, GameType gmode)
    {
        _tempStats.Clear();
        var reader = new StringReader(nText);
        while((nText = reader.ReadLine()) != null)
        {
            nText = nText.Replace(" ", "");
            if (nText == "")
            {
                continue;
            }
            string statType = Regex.Replace(nText, "[^A-Za-z]", "");
            string statValue = Regex.Replace(nText, "[^0-9]", "");
            //  Console.WriteLine(gmode + " : Found line with Type of {0} and a value of {1}",statType,statValue);
            _tempStats.Add(new KeyValuePair<GameType, KeyValuePair<string, string>>(gmode, new KeyValuePair<string, string>(statType, statValue)));
        }
    }

您的解决方案很好,但可读性不太好

你可以这样做:

private readonly List<GameStats> _tempStats = new List<GameStats>();
并将其添加到您的列表中,如:

_tempStats.Add(new GameStats(gmode, statType, statValue));

我想,这看起来不错,也可以。钥匙是独一无二的吗?如果是这样的话,那么字典呢?为什么不用这些数据创建一个游戏类,并将该类放在一个列表中呢?您有2个字段和一个枚举器…我从中获取数据的表是动态的,可以找到大约40个不同的键,值是动态的,大约在1到10000之间。为了将来参考,您可以将整个代码块缩进4个空格,以使其正确格式化为块代码段。
_tempStats.Add(new GameStats(gmode, statType, statValue));