c#遍历HashTable

foreach (System.Collections.DictionaryEntry objDE in objHasTab)

{

Console.WriteLine(objDE.Key.ToString());

Console.WriteLine(objDE.Value.ToString());

}

System.Collections.IDictionaryEnumerator enumerator = objHashTablet.GetEnumerator();

while (enumerator.MoveNext())

{

Console.WriteLine(enumerator.Key); // Hashtable关健字

Console.WriteLine(enumerator.Value); // Hashtable值

}

查看缓存项

Cache.Insert("key1", "key1");

Cache.Insert("key2", "key2");

Cache.Insert("key3", "key3");

StringBuilder sb = new StringBuilder();

sb.Append("缓存中的项数:").Append(Cache.Count.ToString() + "</br>");

foreach (DictionaryEntry Caches in Cache)

{

sb.Append("键:").Append(Caches.Key.ToString());

sb.Append("值:").Append(Caches.Value.ToString()).Append("</br>");

}

Response.Write(sb.ToString());

Cache.Remove("key1");

Cache.Remove("key2");

Cache.Remove("key3");

  

检索缓存项

string cachedString;

cachedString = (string)Cache["CacheItem"];

if (cachedString == null)

{

Response.Write("缓存项不存在,添加!");

cachedString = "Hello, World.";

Cache.Insert("CacheItem", cachedString);

}

说明:

(1) 通过在Cache对象中进行检查来确定该项是否不为null,如果该项存在,则将它分配给变量。否则,重新创建该项,将它添加到缓存中,然后访问它。