C#程序访问词典中的第一个元素
以下是我们的字典与一些元素-
Dictionary<int, string> d = new Dictionary<int, string>() {{1,"Electronics"},
{2, "Clothing"},
{3,"Toys"},
{4,"Footwear"},
{5, "Accessories"}
};
现在要显示第一个元素,请像这样设置键。
d[1];
上面显示了第一个元素。
示例
using System;using System.Collections.Generic;
public class Program {
public static void Main() {
Dictionary<int, string> d = new Dictionary<int, string>() {
{1,"Electronics"},
{2, "Clothing"},
{3,"Toys"},
{4,"Footwear"},
{5, "Accessories"}
};
foreach (KeyValuePair<int, string> ele in d) {
Console.WriteLine("Key = {0}, Value = {1}", ele.Key, ele.Value);
}
Console.WriteLine("First element: "+d[1]);
}
}
输出结果
Key = 1, Value = ElectronicsKey = 2, Value = Clothing
Key = 3, Value = Toys
Key = 4, Value = Footwear
Key = 5, Value = Accessories
First element: Electronics
以上是 C#程序访问词典中的第一个元素 的全部内容, 来源链接: utcz.com/z/331008.html