Copy the values from Hashtable into an array using the CopyTo() method and then display the array contents : Hashtable : Data Structure C# Examples


C# Examples » Data Structure » Hashtable »

 

Copy the values from Hashtable into an array using the CopyTo() method and then display the array contents









    
using  System;
using  System.Collections;

class  MainClass
{

    public  static  void  Main()
    {
        Hashtable  myHashtable  =  new  Hashtable();

        myHashtable.Add("AL",  "Alabama");
        myHashtable.Add("CA",  "California");
        myHashtable.Add("FL",  "Florida");
        myHashtable.Add("NY",  "New  York");
        myHashtable.Add("WY",  "Wyoming");

        foreach  (string  myKey  in  myHashtable.Keys)
        {
            Console.WriteLine("myKey  =  "  +  myKey);
        }
        foreach(string  myValue  in  myHashtable.Values)
        {
            Console.WriteLine("myValue  =  "  +  myValue);
        }
        
        Console.WriteLine("Copying  values  to  myValues  array");
        string[]  myValues  =  new  string[5];
        myHashtable.Values.CopyTo(myValues,  0);
        for  (int  counter  =  0;  counter  <  myValues.Length;  counter++)
        {
            Console.WriteLine("myValues["  +  counter  +  "]  =  "  +  myValues[counter]);
        }
    }
}
    
   
  
   



Output

myKey = NY
myKey = CA
myKey = FL
myKey = WY
myKey = AL
myValue = New York
myValue = California
myValue = Florida
myValue = Wyoming
myValue = Alabama
Copying values to myValues array
myValues[0] = New York
myValues[1] = California
myValues[2] = Florida
myValues[3] = Wyoming
myValues[4] = Alabama


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Data Structure
» Hashtable