我试了,可是报错:无法将类型“object”隐式转换为string。
我不知道显式转换该如何写,请高手指教。
谢谢楼上,中秋快乐。

解决方案 »

  1.   

    MSDN中
    示例
    下列示例说明如何创建和初始化 Hashtable,以及如何打印出其键和值。
    [Visual Basic] 
    Imports System
    Imports System.Collections
    Imports Microsoft.VisualBasicPublic Class SamplesHashtable    
        
        Public Shared Sub Main()
            
            ' Creates and initializes a new Hashtable.
            Dim myHT As New Hashtable()
            myHT.Add("First", "Hello")
            myHT.Add("Second", "World")
            myHT.Add("Third", "!")
            
            ' Displays the properties and values of the Hashtable.
            Console.WriteLine("myHT")
            Console.WriteLine("  Count:    {0}", myHT.Count)
            Console.WriteLine("  Keys and Values:")
            PrintKeysAndValues(myHT)
        End Sub
        
        Public Shared Sub PrintKeysAndValues(myList As Hashtable)
            Dim myEnumerator As IDictionaryEnumerator = myList.GetEnumerator()
            Console.WriteLine(ControlChars.Tab + "-KEY-" + ControlChars.Tab _
               + "-VALUE-")
            While myEnumerator.MoveNext()
                Console.WriteLine(ControlChars.Tab + "{0}:" + ControlChars.Tab _
                   + "{1}", myEnumerator.Key, myEnumerator.Value)
            End While
            Console.WriteLine()
        End Sub
    End Class' This code produces the following output.

    ' myHT
    '   Count:    3
    '   Keys and Values:
    '     -KEY-    -VALUE-
    '     Third:    !
    '     Second:    World
    '     First:    Hello
    [C#] 
    using System;
    using System.Collections;
    public class SamplesHashtable  {   public static void Main()  {      // Creates and initializes a new Hashtable.
          Hashtable myHT = new Hashtable();
          myHT.Add("First", "Hello");
          myHT.Add("Second", "World");
          myHT.Add("Third", "!");      // Displays the properties and values of the Hashtable.
          Console.WriteLine( "myHT" );
          Console.WriteLine( "  Count:    {0}", myHT.Count );
          Console.WriteLine( "  Keys and Values:" );
          PrintKeysAndValues( myHT );
       }
       public static void PrintKeysAndValues( Hashtable myList )  {
          IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
          Console.WriteLine( "\t-KEY-\t-VALUE-" );
          while ( myEnumerator.MoveNext() )
             Console.WriteLine("\t{0}:\t{1}", myEnumerator.Key, myEnumerator.Value);
          Console.WriteLine();
       }
    }
    /* 
    This code produces the following output.myHT
      Count:    3
      Keys and Values:
        -KEY-    -VALUE-
        Third:    !
        Second:    World
        First:    Hello
    */