技术开发 频道

C#的6种常用集合类大比拼

   (6)NameValueCollection类

  官方给NameValueCollection定义为特殊集合一类,在System.Collections.Specialized下。

  System.Collections.Specialized下还有HybridDicionary类,建议少于10个元素用HybridDicionary,当元素增加会自动转为HashTable。

  System.Collections.Specialized下还有HybridDicionary类,字符串集合。

  System.Collections.Specialized下还有其他类大家可以各取所需!

  言归正转主要说NameValueCollection,HashTable 和 NameValueCollection很类似但是他们还是有区别的,HashTable 的KEY是唯一性,而NameValueCollection则不唯一!

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
namespace ConsoleApplication1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            System.Collections.Hashtable ht
= new System.Collections.Hashtable();
            ht.Add(
"DdpMDisplaySeq".Trim(), "Display Sequence".Trim());
            ht.Add(
"DdpMNameChi".Trim(), "Name (Chinese)".Trim());
            ht.Add(
"DdpMNameEng".Trim(), "Name (English)".Trim());
            ht.Add(
"Comment".Trim(), "Comment".Trim());
            ht.Add(
"DdpMMarketCode".Trim(), "Market Code".Trim());
            
foreach (object key in ht.Keys)
            {
                Console.WriteLine(
"{0}/{1}    {2},{3}", key, ht[key], key.GetHashCode(), ht[key].GetHashCode());
            }
            Console.WriteLine(
" ");//清清月儿 http://blog.csdn.net/21aspnet/
            NameValueCollection myCol = new NameValueCollection();
            myCol.Add(
"DdpMDisplaySeq".Trim(), "Display Sequence".Trim());
            myCol.Add(
"DdpMNameChi".Trim(), "Name (Chinese)".Trim());
            myCol.Add(
"DdpMNameChi".Trim(), "Name (English)".Trim());
            myCol.Add(
"Comment".Trim(), "Comment".Trim());
            myCol.Add(
"DdpMMarketCode".Trim(), "Market Code".Trim());
            
foreach (string key in myCol.Keys)
            {
                Console.WriteLine(
"{0}/{1} {2},{3}", key, myCol[key], key.GetHashCode(), myCol[key].GetHashCode());
            }

        }

    }
}
0
相关文章