我有一个关于C#中列表和字典的奇怪疑问。
在列表中,我们使用以下方法添加项目到列表中:
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> list = new List<int>();
list.Add(2);
list.Add(3);
list.Add(5);
list.Add(7);
}
}
在字典中,我们像这样添加元素...
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Dictionary<string, int> d = new Dictionary<string, int>();
d.Add("cat", 2);
d.Add("dog", 1);
d.Add("llama", 0);
d.Add("iguana", -1);
}
}
我不太清楚它们的区别,但在字典中,我们像添加(键,值)对一样添加项目,而在列表中,我们只是添加项目而没有指定任何键...
有人能解释一下吗?