字典类

1.NameValueCollection 

2.Hashtable

3.Dictionary

 

NameValueCollection
表示可通过键或索引访问的关联 String 键和 String 值的集合。
命名空间:System.Collections.Specialized


Hashtable
表示根据键的哈希代码进行组织的键/值对的集合。
命名空间:System.Collections


Dictionary<TKey, TValue>
表示键和值的集合。
命名空间:System.Collections.Generic

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;using System.Text;
namespace Test
{
    class Class1
    {
        public static void Test_NameValueCollection()
        {
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("1", "Monday");
            nvc.Add("2", "Tuesday");
            nvc.Add("3", "Wednesday");
            nvc.Add("4", "Thursday");
            nvc.Add("5", "Friday");
            nvc.Add("6", "Saturday");
            nvc.Add("0", "Sunday");

            //遍历方式一
            for (int i = 0; i < nvc.Count; i++)
            {
                Console.WriteLine("{0} - {1}", nvc.GetKey(i), nvc.Get(i));
            }

            //遍历方式二
            foreach (string key in nvc)
            {
                Console.WriteLine("{0} - {1}", key, nvc[key]);
            }

            /*
nvc
{System.Collections.Specialized.NameValueCollection}
    base: {System.Collections.Specialized.NameValueCollection}
    AllKeys: {string[7]}
             
             */
        }

        public static void Test_Hashtable()
        {
            Hashtable ht = new Hashtable();
            ht.Add("1", "Monday");
            ht.Add("2", "Tuesday");
            ht.Add("3", "Wednesday");
            ht.Add("4", "Thursday");
            ht.Add("5", "Friday");
            ht.Add("6", "Saturday");
            ht.Add("0", "Sunday");

            //遍历方式一
            foreach (DictionaryEntry de in ht)
            {
                Console.WriteLine("{0} - {1}", de.Key, de.Value);
            }

            //遍历方式二
            foreach (var key in ht.Keys)
            {
                Console.WriteLine("{0} - {1}", key, ht[key]);
            }

            //遍历方式三
            IDictionaryEnumerator ide = ht.GetEnumerator();
            bool flag = ide.MoveNext();
            while (flag)
            {
                Console.WriteLine("{0} - {1}", ide.Key, ide.Value);
                flag = ide.MoveNext();
            }
/* ht Count = 7 ["0"]: "Sunday" ["1"]: "Monday" ["2"]: "Tuesday" ["3"]: "Wednesday" ["4"]: "Thursday" ["5"]: "Friday" ["6"]: "Saturday" */ } public static void Test_Dictionary() { Dictionary<string, object> dict = new Dictionary<string, object>(); dict.Add("1", "Monday"); dict.Add("2", "Tuesday"); dict.Add("3", "Wednesday"); dict.Add("4", "Thursday"); dict.Add("5", "Friday"); dict.Add("6", "Saturday"); dict.Add("0", "Sunday"); // foreach (var item in dict) { //KeyValuePair<string, object> Console.WriteLine("{0} - {1}", item.Key, item.Value); } /* dict Count = 7 [0]: {[1, Monday]} [1]: {[2, Tuesday]} [2]: {[3, Wednesday]} [3]: {[4, Thursday]} [4]: {[5, Friday]} [5]: {[6, Saturday]} [6]: {[0, Sunday]} */ } } }

 

posted @ 2017-07-28 23:47  茗::流  阅读(137)  评论(1)    收藏  举报
如有雷同,纯属参考。如有侵犯你的版权,请联系我。