C# 字典中添加 List:详细指南和示例
在 C# 中,可以使用Dictionary<TKey, TValue> 类来实现字典功能,其中TKey 表示字典的键类型,TValue 表示字典的值类型。要向字典中添加List<T> 类型的值,可以使用以下代码:
Dictionary<string, List<int>> dict = new Dictionary<string, List<int>>();
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
dict.Add('key', list);
在这个例子中,我们创建了一个Dictionary<string, List<int>> 类型的字典,其中键的类型是string,值的类型是List<int>。然后我们创建了一个List<int> 类型的列表,并向其中添加了三个整数。最后,我们将这个列表作为值添加到字典中,键是'key'。
原文地址: https://www.cveoy.top/t/topic/lo9v 著作权归作者所有。请勿转载和采集!