C# hashtable如何更新内部所有value

主要实现代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;

  6. namespace AlignForHashtable
  7. {
  8.     class Program
  9.     {
  10.         private static Hashtable mHTable = new Hashtable();

  11.         private static void initHTable()
  12.         {
  13.             mHTable.Clear();
  14.             for (int i = 0; i 10; i++)
  15.             {
  16.                 mHTable.Add(i, "Value" + i);
  17.             }
  18.         }

  19.         // Batch update all the element value .....
  20.         private static void updateHashtableValue(string newValue)
  21.         {
  22.             Hashtable htable = new Hashtable();
  23.             foreach (DictionaryEntry de in mHTable)
  24.             {
  25.                 string valueStr = newValue;
  26.                 htable.Add(de.Key, valueStr);
  27.             }

  28.             mHTable.Clear();
  29.             mHTable = htable;

  30.             /* Error Usage ... */
  31.             //Hashtable htable = this.mHTable;
  32.             //this.mHTable.Clear();
  33.             //string valueStr = string.Empty;

  34.             //foreach (DictionaryEntry de in htable)
  35.             //{
  36.             // valueStr = newValue;
  37.             // this.mHTable.Add(de.Key, valueStr);
  38.             //}
  39.         }

  40.         static void Main(string[] args)
  41.         {
  42.             
  43.             Console.WriteLine("Before update ..... \n");
  44.             initHTable();

  45.             foreach (DictionaryEntry de in mHTable)
  46.             {
  47.                 Console.WriteLine("Key={0},Value={1}", de.Key, de.Value);
  48.             }

  49.             updateHashtableValue("NBA all stars...");

  50.             Console.WriteLine();
  51.             Console.WriteLine("After update ..... \n");

  52.             foreach (DictionaryEntry de in mHTable)
  53.             {
  54.                 Console.WriteLine("Key={0},Value={1}", de.Key, de.Value);
  55.             }

  56.             Console.ReadLine();
  57.         }

  58.     }
  59. }

image

注意!上面代码中,屏蔽代码的更新方法是错误的。

需要留意!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值