C# Dictionary的使用实例代码
class dirctonary
{
public void dictionaryget()
{
dictionary<int, string> productlist = new system.collections.generic.dictionary<int, string>();
productlist.add(1, "productionone");
productlist.add(2, "productiontwo");
foreach (keyvaluepair<int, string> production in productlist)
{
messagebox.show(string.format("{0},{1}", production.key, production.value));
}
//messagebox.show(productlist.count.tostring());
//messagebox.show(productlist[1].tostring());
dictionary<int, string>.keycollection keys = productlist.keys;
foreach (var item in keys)
{
messagebox.show(item.tostring());
}
dictionary<int, string>.valuecollection collection = productlist.values;
foreach (var item in collection)
{
messagebox.show(string.format("{0}", item));
}
//productlist.remove(1);
//productlist.clear();
messagebox.show("判断是否包含键值对中的键为”1“的值");
if (productlist.containskey(1))
{
messagebox.show(productlist[1]);
}
messagebox.show("判断是否包含键值对中的值为”productiontwo“的值");
if (productlist.containsvalue("productiontwo"))
{
messagebox.show(string.format("{0}", "this really exists"));
}
}