我对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);
}
}
Run Code Online (Sandbox Code Playgroud)
在字典中我们添加这样的项目......
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我不确切地知道有什么区别,但是在字典中我们添加像(键,值)对的项目,在列表中我们只是添加项而不指定任何键.
有人会澄清这个吗?