Csharp/C Sharp by API/System.Collections/HashTable — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:08, 26 мая 2010

Hashtable.Add

  
using System;
using System.Collections;
public class Starter {
    public static void Main() {
        Hashtable zHash = new Hashtable();
        zHash.Add("one", 1);
        zHash.Add("two", 2);
        zHash.Add("three", 3);
        zHash.Add("four", 4);
        IDictionaryEnumerator e = zHash.GetEnumerator();
        while (e.MoveNext()) {
            Console.WriteLine(
                "{0} {1}",
                e.Key, e.Value);
        }
    }
}


Hashtable.ContainsKey

  
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);
    }
    
    if (myHashtable.ContainsKey("FL"))
    {
      Console.WriteLine("myHashtable contains the key FL");
    }
  }
}


Hashtable.ContainsValue

  
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);
    }
    
    if (myHashtable.ContainsValue("Florida"))
    {
      Console.WriteLine("myHashtable contains the value Florida");
    }
  }
}


HashTable.Keys

  
using System; 
using System.Collections; 
 
class HashtableDemo { 
  public static void Main() { 
    Hashtable ht = new Hashtable(); 
     
    
    ht.Add("a", "A"); 
    ht.Add("b", "B"); 
    ht.Add("c", "C"); 
    ht.Add("e", "E"); 
 
    // Get a collection of the keys. 
    ICollection c = ht.Keys; 
 
    foreach(string str in c) 
      Console.WriteLine(str + ": " + ht[str]); 
  } 
}


Hashtable.Keys.CopyTo

  
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 keys to myKeys array");
    string[] myKeys = new string[5];
    myHashtable.Keys.CopyTo(myKeys, 0);
    
    for (int counter = 0; counter < myKeys.Length; counter++) {
      Console.WriteLine("myKeys[" + counter + "] = " + myKeys[counter]);
    }
  }
}


HashTable.Remove

  
using System;
using System.Collections;
class MainClass
{
  static void Main(string[] args)
  {
    Hashtable a = new Hashtable(10);
          
    a.Add(100, "A");
    a.Add(200, "C");
    
    a.Remove(100);
    a.Remove(200);
  }
}


Hashtable.Synchronized

  
using System;
using System.Collections;
class MainClass
{
  static void Main(string[] args)
  {
    Hashtable a = new Hashtable(10);
    Hashtable.Synchronized(a);
          
  }
}


Hashtable.Values

  
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);
    }
    
    if (myHashtable.ContainsKey("FL"))
    {
      Console.WriteLine("myHashtable contains the key FL");
    }
  }
}


Hashtable.Values.CopyTo

  
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]);
    }
  }
}