主要实现代码如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
-
- namespace AlignForHashtable
- {
- class Program
- {
- private static Hashtable mHTable = new Hashtable();
-
- private static void initHTable()
- {
- mHTable.Clear();
- for (int i = 0; i 10; i++)
- {
- mHTable.Add(i, "Value" + i);
- }
- }
-
- // Batch update all the element value .....
- private static void updateHashtableValue(string newValue)
- {
- Hashtable htable = new Hashtable();
- foreach (DictionaryEntry de in mHTable)
- {
- string valueStr = newValue;
- htable.Add(de.Key, valueStr);
- }
-
- mHTable.Clear();
- mHTable = htable;
-
- /* Error Usage ... */
- //Hashtable htable = this.mHTable;
- //this.mHTable.Clear();
- //string valueStr = string.Empty;
-
- //foreach (DictionaryEntry de in htable)
- //{
- // valueStr = newValue;
- // this.mHTable.Add(de.Key, valueStr);
- //}
- }
-
- static void Main(string[] args)
- {
-
- Console.WriteLine("Before update ..... \n");
- initHTable();
-
- foreach (DictionaryEntry de in mHTable)
- {
- Console.WriteLine("Key={0},Value={1}", de.Key, de.Value);
- }
-
- updateHashtableValue("NBA all stars...");
-
- Console.WriteLine();
- Console.WriteLine("After update ..... \n");
-
- foreach (DictionaryEntry de in mHTable)
- {
- Console.WriteLine("Key={0},Value={1}", de.Key, de.Value);
- }
-
- Console.ReadLine();
- }
-
- }
- }
注意!上面代码中,屏蔽代码的更新方法是错误的。
需要留意!!