我有一个包含键和值的字典.我想打印到控制台的键和值.但我想分别打印键和值.y是唯一的,m是分配给键的值.
看到我在这里尝试如何做到这一点,但我得到一个错误说
预期方法,代表或事件
码:
List<int> col = new List<int>();
Dictionary<int, int> dic = new Dictionary<int, int>();
//iterates through min-max
for (int y = 0; y <= 15; y++)
{
int m = 0;
//iterates through array
for (int i = 0; i < Arr.Length; i++)
{
//comparisons
if (y == Arr[i])
{
m++;
}
}
dic.Add(y,m);
Console.WriteLine("Number {0} appears {1} times ", y, dic[y]);
Run Code Online (Sandbox Code Playgroud)
您可以使用索引器dic[something]操作来获取给定键的值.
Dictionary<int, int> dic = new Dictionary<int, int>();
//rest of my code here
{...}
dic.Add(y,m);
Console.WriteLine("Number {0} appears {1} times ", y, dic[y]);
Run Code Online (Sandbox Code Playgroud)