这个Hashtable好象很复杂,有没有简单一点的能记录几个键值对的集合类啊

解决方案 »

  1.   

    if(myHT==null)
    {
    myHT = new System.Collections.Hashtable();
    myHT.Add("哈哈","呵呵");
    }这样就搞定了,我把他搞成局部变量了,我哭,我的一百分哪
      

  2.   


    能给我个windows服务的例子吗?想学习。
      

  3.   

    这是sdk中的例子,应该比较简单,楼主是不是其他原因?
    using System;
    using System.Collections;
    public class SamplesHashtable  {   public static void Main()  {      // Creates and initializes a new Hashtable.
          Hashtable myHT = new Hashtable();
          myHT.Add( 0, "zero" );
          myHT.Add( 1, "one" );
          myHT.Add( 2, "two" );
          myHT.Add( 3, "three" );
          myHT.Add( 4, "four" );      // Displays the values of the Hashtable.
          Console.WriteLine( "The Hashtable contains the following values:" );
          PrintIndexAndKeysAndValues( myHT );      // Searches for a specific key.
          int myKey = 2;
          Console.WriteLine( "The key \"{0}\" is {1}.", myKey, myHT.ContainsKey( myKey ) ? "in the Hashtable" : "NOT in the Hashtable" );
          myKey = 6;
          Console.WriteLine( "The key \"{0}\" is {1}.", myKey, myHT.ContainsKey( myKey ) ? "in the Hashtable" : "NOT in the Hashtable" );      // Searches for a specific value.
          String myValue = "three";
          Console.WriteLine( "The value \"{0}\" is {1}.", myValue, myHT.ContainsValue( myValue ) ? "in the Hashtable" : "NOT in the Hashtable" );
          myValue = "nine";
          Console.WriteLine( "The value \"{0}\" is {1}.", myValue, myHT.ContainsValue( myValue ) ? "in the Hashtable" : "NOT in the Hashtable" );
       }
       public static void PrintIndexAndKeysAndValues( Hashtable myList )  {
          IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
          int i = 0;
          Console.WriteLine( "\t-INDEX-\t-KEY-\t-VALUE-" );
          while ( myEnumerator.MoveNext() )
             Console.WriteLine( "\t[{0}]:\t{1}\t{2}", i++, myEnumerator.Key, myEnumerator.Value );
          Console.WriteLine();
       }
    }
    /* 
    This code produces the following output.The Hashtable contains the following values:
        -INDEX-    -KEY-    -VALUE-
        [0]:    4    four
        [1]:    3    three
        [2]:    2    two
        [3]:    1    one
        [4]:    0    zeroThe key "2" is in the Hashtable.
    The key "6" is NOT in the Hashtable.
    The value "three" is in the Hashtable.
    The value "nine" is NOT in the Hashtable.
    */