新人关于hashtable的简单的问题
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");
// 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);
}
}
}
以上为MSDN范例,里面使用foreach (DictionaryEntry de in openWith)遍历了Hashtable的内容,但怎么看出Hashtable对象是DictionaryEntry类构成的呢,从对象浏览器内只能查到Hashtable支持public virtual System.Collections.IDictionaryEnumerator GetEnumerator(),从IDictionaryEnumerator得到一个DictionaryEntry类对象阿,这怎么来理解呢
[解决办法]
凡是能用 foreach遍历的 都是 实现了 IEnumerable 借口的..
反之则不能用foreach遍历
[解决办法]