hashtable的应用
Code
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public enum InstrumentType
{
Guitar,
Mandolin,
BanJo
}
public enum Builder
{
me,
you,
he
}
public enum Strings
{
five,
seven,
eight,
eleven
}
public class InstrumentSpec
{
private Hashtable properties;
public InstrumentSpec(Hashtable _properties)
{
if (_properties == null)
{
this.properties = new Hashtable();
}
else
{
properties = new Hashtable(_properties);
}
}
//注意这里返回值是object说明字典在存储过程中将value装箱了
public object Getproperty(string name)
{
return properties[name];
}
public Hashtable GetPorperties()
{
return properties;
}
public bool match(InstrumentSpec otherSpec)
{
Hashtable otherproperties = otherSpec.GetPorperties();
foreach (DictionaryEntry de in otherproperties)
{
//因此这里比较的时候要tostring()
if (properties[de.Key].ToString() != otherproperties[de.Key].ToString())
{
return false;
}
}
return true;
}
}
public class Invention
{
public List<InstrumentSpec> collections=new List<InstrumentSpec>();
public void addInstrument(InstrumentSpec spec)
{
collections.Add(spec);
}
public void initial(Invention invention)
{
Hashtable properties = new Hashtable();
properties.Add("instrumentType", InstrumentType.BanJo);
properties.Add("builder",Builder.he);
properties.Add("strings",Strings.eight);
invention.addInstrument(new InstrumentSpec(properties));
Hashtable properties1 = new Hashtable();
properties1.Add("instrumentType",InstrumentType.Guitar);
properties1.Add("builder",Builder.me);
properties1.Add("strings",Strings.eight);
invention.addInstrument(new InstrumentSpec(properties1));
Hashtable properties2 = new Hashtable();
properties2.Add("instrumentType",InstrumentType.BanJo);
properties2.Add("builder",Builder.he);
properties2.Add("strings",Strings.eleven);
invention.addInstrument(new InstrumentSpec(properties2));
}
}
class Program
{
static void Main(string[] args)
{
Invention invention = new Invention();
Hashtable properties = new Hashtable();
properties.Add("instrumentType", InstrumentType.BanJo);
properties.Add("builder", Builder.he);
properties.Add("strings", Strings.eight);
invention.addInstrument(new InstrumentSpec(properties));
Hashtable properties1 = new Hashtable();
properties1.Add("instrumentType", InstrumentType.Guitar);
properties1.Add("builder", Builder.me);
properties1.Add("strings", Strings.eight);
invention.addInstrument(new InstrumentSpec(properties1));
Hashtable properties2 = new Hashtable();
properties2.Add("instrumentType", InstrumentType.BanJo);
properties2.Add("builder", Builder.he);
properties2.Add("strings", Strings.eleven);
invention.addInstrument(new InstrumentSpec(properties2));
Hashtable otherpro = new Hashtable();
otherpro.Add("instrumentType", InstrumentType.BanJo);
foreach (DictionaryEntry de in otherpro)
{
Console.WriteLine(otherpro[de.Key]);
}
Console.WriteLine("");
InstrumentSpec otherSpec = new InstrumentSpec(otherpro);
foreach (InstrumentSpec i in invention.collections)
{
if (i.match(otherSpec))
{
Console.WriteLine("i can found this for you:");
Hashtable properties3 = i.GetPorperties();
foreach (DictionaryEntry de in properties3)
{
Console.WriteLine("{0}:{1}",de.Key,i.Getproperty(de.Key.ToString()));
}
Console.WriteLine("");
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections;
class Example
{
public static void Main()
{
// Create a new hash table.
//
Hashtable openWith = new Hashtable();
// Add some elements to the hash table. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// The Add method throws an exception if the new key is
// already in the hash table.
try
{
openWith.Add("txt", "winword.exe");
}
catch
{
Console.WriteLine("An element with Key = \"txt\" already exists.");
}
// The Item property is the default property, so you
// can omit its name when accessing elements.
Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
// The default Item property can be used to change the value
// associated with a key.
openWith["rtf"] = "winword.exe";
Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);
// If a key does not exist, setting the default Item property
// for that key adds a new key/value pair.
openWith["doc"] = "winword.exe";
// ContainsKey can be used to test keys before inserting
// them.
if (!openWith.ContainsKey("ht"))
{
openWith.Add("ht", "hypertrm.exe");
Console.WriteLine("Value added for key = \"ht\": {0}", openWith["ht"]);
}
// When you use foreach to enumerate hash table elements,
// the elements are retrieved as KeyValuePair objects.
Console.WriteLine();
foreach( DictionaryEntry de in openWith )
{
Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
}
// To get the values alone, use the Values property.
ICollection valueColl = openWith.Values;
// The elements of the ValueCollection are strongly typed
// with the type that was specified for hash table values.
Console.WriteLine();
foreach( string s in valueColl )
{
Console.WriteLine("Value = {0}", s);
}
// To get the keys alone, use the Keys property.
ICollection keyColl = openWith.Keys;
// The elements of the KeyCollection are strongly typed
// with the type that was specified for hash table keys.
Console.WriteLine();
foreach( string s in keyColl )
{
Console.WriteLine("Key = {0}", s);
}
// Use the Remove method to remove a key/value pair.
Console.WriteLine("\nRemove(\"doc\")");
openWith.Remove("doc");
if (!openWith.ContainsKey("doc"))
{
Console.WriteLine("Key \"doc\" is not found.");
}
}
}
/* This code example produces the following output:
An element with Key = "txt" already exists.
For key = "rtf", value = wordpad.exe.
For key = "rtf", value = winword.exe.
Value added for key = "ht": hypertrm.exe
Key = dib, Value = paint.exe
Key = txt, Value = notepad.exe
Key = ht, Value = hypertrm.exe
Key = bmp, Value = paint.exe
Key = rtf, Value = winword.exe
Key = doc, Value = winword.exe
Value = paint.exe
Value = notepad.exe
Value = hypertrm.exe
Value = paint.exe
Value = winword.exe
Value = winword.exe
Key = dib
Key = txt
Key = ht
Key = bmp
Key = rtf
Key = doc
Remove("doc")
Key "doc" is not found.
*/