ASP.NET Dictionary 的基本用法示例介绍
程序员文章站
2024-02-28 09:34:16
复制代码 代码如下: dictionary o_dic = new dictionary
dictionary<string, string> o_dic = new dictionary<string, string>();
//添加元素
o_dic.add("01", "aaa");
o_dic.add("02","bbb");
//判断某个key是否存在
if (!o_dic.containskey("03"))
{
o_dic.add("03", "ccc");
}
//移除某个
o_dic.remove("03");
//取某个的值
string a = o_dic["02"];
//遍歷
foreach (keyvaluepair<string, string> kvp in o_dic)
{
response.write(kvp.key + "," + kvp.value);
}
//遍歷key
foreach (string s in o_dic.keys) { }
//遍歷value
foreach (string s in o_dic.values) { }
复制代码 代码如下:
dictionary<string, string> o_dic = new dictionary<string, string>();
//添加元素
o_dic.add("01", "aaa");
o_dic.add("02","bbb");
//判断某个key是否存在
if (!o_dic.containskey("03"))
{
o_dic.add("03", "ccc");
}
//移除某个
o_dic.remove("03");
//取某个的值
string a = o_dic["02"];
//遍歷
foreach (keyvaluepair<string, string> kvp in o_dic)
{
response.write(kvp.key + "," + kvp.value);
}
//遍歷key
foreach (string s in o_dic.keys) { }
//遍歷value
foreach (string s in o_dic.values) { }