引用类型的对象的成员也是引用类型的:
View Code
using System; using System.Collections; namespace HashTableDemo { class Program { class Employee { public string Name; public int Age; public Employee(string name, int age) { Name = name; Age = age; } } static void Main(string[] args) { Hashtable tClass = new Hashtable(); tClass.Add("张三".GetHashCode(), new Employee("张三", 18)); tClass.Add("李四".GetHashCode(), new Employee("李四", 18)); object obj = tClass["张三".GetHashCode()]; (obj as Employee).Age = 22; foreach (DictionaryEntry item in tClass) { var tmp = item.Value as Employee; if (tmp.Name.Equals("李四")) { tmp.Age = 19; } } foreach (DictionaryEntry item in tClass) { var tmp = item.Value as Employee; Console.WriteLine(tmp.Name + ":" + tmp.Age); } } } }
运行结果:
张三:22
李四:19
请按任意键继续. . .