Items can also be added to a Hashtable using string indexes:
Hashtable table = new Hashtable();
table["Sunday"] = "星期天";
table["Monday"] = "星期一";
table["Tuesday"] = "星期二";
...
there's a difference between adding items with Add and adding them with indexers. Add throws an exception if the key you pass to it is already in the table. Indexers don't. They simply replace the old value with the new one.
public class Example
{
public static void Main()
{
// Create a new dictionary of strings, with integer keys.
Dictionary<int, string> d = new Dictionary<int, string>();
// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
d.Add(1, "a");
d.Add(3, "abc");
d.Add(7, "abcdefg");
d.Add(19, "abc");
// The Add method throws an exception if the new key is
// already in the dictionary.
try
{
d.Add(7, "xxx");
}
catch (ArgumentException ae)
{
Console.WriteLine("An element with Key = 7 already exists.");
}
// The Item property is another name for the indexer, so you
// can omit its name when accessing elements.
Console.WriteLine("For key = 19, value = {0}.", d[19]);
// The indexer can be used to change the value associated
// with a key.
d[19] = "abcdefghijklmnopqrs";
Console.WriteLine("For key = 19, value = {0}.", d[19]);
// If a key does not exist, setting the indexer for that key
// adds a new key/value pair.
d[11] = "abcdefghijk";
// The indexer throws an exception if the requested key is
// not in the dictionary.
try
{
Console.WriteLine("For key = 5, value = {0}.", d[5]);
}
catch (KeyNotFoundException knfe)
{
Console.WriteLine("Key = 5 is not found.");
}
// When a program often has to try keys that turn out not to
// be in the dictionary, TryGetValue is the most efficient
// way to retrieve values.
string value = "";
if (d.TryGetValue(5, out value))
{
Console.WriteLine("For key = 5, value = {0}.", value);
}
else
{
Console.WriteLine("Key = 5 is not found.");
}
// ContainsKey is not an efficient way to retrieve values,
// but it's a good way to test keys before inserting them.
if (!d.ContainsKey(5))
{
d.Add(5, "abcde");
Console.WriteLine("Value added for key = 5: {0}", d[5]);
}
// When you use foreach to enumerate dictionary elements,
// the elements are retreived as KeyValuePair objects.
Console.WriteLine();
foreach( KeyValuePair<int, string> kvp in d )
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
// To get the values alone, use the Values property.
Console.WriteLine();
Dictionary<int, string>.ValueCollection dvc = d.Values;
// The elements of the ValueCollection are strongly typed
// with the type that was specified for dictionary values.
foreach( string s in dvc )
{
Console.WriteLine("Value = {0}", s);
}
}
}
/* This code example produces the following output:
An element with Key = 7 already exists.
For key = 19, value = abc.
For key = 19, value = abcdefghijklmnopqrs.
Key = 5 is not found.
Key = 5 is not found.
Value added for key = 5: abcde
Key = 1, Value = a
Key = 3, Value = abc
Key = 7, Value = abcdefg
Key = 19, Value = abcdefghijklmnopqrs
Key = 11, Value = abcdefghijk
Key = 5, Value = abcde
Value = a
Value = abc
Value = abcdefg
Value = abcdefghijklmnopqrs
Value = abcdefghijk
Value = abcde
*/
to weisunding(鼎鼎):这样写在java中可以,在c#中不行
to crossrowman(godi):我想知道System.Collections.Hashtable 是否内置支持泛型
to Macosx(总算毕业了) :正确的语法是什么呢?
to namhyuk(namhyuk) : 您说的很清楚,但这不是我想问的问题