欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# Dictionary的使用实例代码

程序员文章站 2023-12-11 18:49:46
复制代码 代码如下:class dirctonary    {      &nbs...

复制代码 代码如下:

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"));
            }
        }

上一篇:

下一篇: