C#程序在字典中查找键
首先,设置带有元素的Dictionary集合。
Dictionary<int, string> d = new Dictionary<int, string>() {
{1,"Applianes"},
{2, "Clothing"},
{3,"Toys"},
{4,"Footwear"},
{5, "Accessories"}
};现在,假设您需要检查密钥5是否存在。为此,使用ContainsKey()方法。如果找到密钥,则返回True。
d.ContainsKey(5);
让我们看完整的代码。
示例
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("Key 5 exists? "+d.ContainsKey(5));
}
}输出结果
Key = 1, Value = Electronics Key = 2, Value = Clothing Key = 3, Value = Toys Key = 4, Value = Footwear Key = 5, Value = Accessories Key 5 exists? True
热门推荐
1 七十年祝福语简短
10 经典新婚祝福语简短英文
11 客户去旅游祝福语简短
12 朋友的孩子祝福语简短
13 住院送饭的祝福语简短
14 书写大赛祝福语范文简短
15 公司生日祝福语员工简短
16 友情祝福语简短小故事
17 10岁高考祝福语简短
18 订婚祝福语简短诗句