如何创建包含一个字符串变量和三个 int 变量的字典

How do create dictionary that holds a string variable and three int variables

我正在尝试编写一些代码,将名称和记录集保存到文本文件中,并且能够读取和编辑。我决定处理这个问题的最佳方法是创建一个字典,将字符串变量作为键,将三个 int 变量作为值。但是,我无法使特定代码起作用。它说它不能包含三个值,尽管它被声明为一个列表。我附上了下面的代码片段。它说错误发生在字典的 {playerwins, computerwins, ties} 部分。

private void FileWriter(string playername, int playerwins, int computerwins, int ties)
        {
            Dictionary<string, List<int>> records = new Dictionary<string, List<int>>()
            {
                {playername, {playerwins, computerwins, ties } };
        }

你不能像这样使用集合初始化器,你需要指定实际的列表类型

new List<int>() {playerwins, computerwins, ties}

固定示例

private void FileWriter(string playername, int playerwins, int computerwins, int ties)
{
   var records = new Dictionary<string, List<int>>
   {
      {playername, new List<int>() {playerwins, computerwins, ties}}
   };
}

另一种选择是使用命名 ValueTuple

var records = new Dictionary<string, (int playerwins, int computerwins, int ties)>
{
   {playername, (playerwins, computerwins, ties)}
};