首页 > 代码库 > c#中Hashtable方法返回值的探索

c#中Hashtable方法返回值的探索

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;namespace ConApp{    class Student    {        string name;        public string Name        {            get { return name; }            set { name = value; }        }        ulong stuID;        public ulong StuID        {            get { return stuID; }            set { stuID = value; }        }        double chinese;        public double Chinese        {            get { return chinese; }            set { chinese = value; }        }        double math;        public double Math        {            get { return math; }            set { math = value; }        }        double english;        public double English        {            get { return english; }            set { english = value; }        }        public double ComputeAvg()        {            return (english + math + chinese) / 3;        }    }    class Program    {        static void Main(string[] args)        {            Student stu = new Student();            stu.Name = "wang";            stu.StuID = 123;            Hashtable ht = new Hashtable();            ht.Add(stu.StuID,stu);            Student stutemp;            foreach (DictionaryEntry de in ht)            {                stutemp = (Student)ht[de.Key];                Console.WriteLine("学生姓名:{0}\n学生学号:{1}\n", stutemp.Name, stutemp.StuID);            }            stu.Name = "";            stu.StuID = 456;            Student temp;            foreach (DictionaryEntry de in ht)            {                temp = (Student)ht[de.Key];                Console.WriteLine("学生姓名:{0}\n学生学号:{1}\n", temp.Name, temp.StuID);            }            ulong id = 123;            temp = (Student)ht[id];            temp.Name = "song";            temp.StuID = 999;            foreach (DictionaryEntry de in ht)            {                stutemp = (Student)ht[de.Key];                Console.WriteLine("学生姓名:{0}\n学生学号:{1}\n", stutemp.Name, stutemp.StuID);            }            Student newStu = new Student();            newStu = (Student)ht[id];            newStu.Name = "uuuu";            newStu.StuID = 1000;            foreach (DictionaryEntry de in ht)            {                stutemp = (Student)ht[de.Key];                Console.WriteLine("学生姓名:{0}\n学生学号:{1}\n", stutemp.Name, stutemp.StuID);            }        }    }}

可以发现,对接收对象的成员改变,原对象的成员也改变。
可以猜想 哈希表内存放的是“引用” 也就是我们c语言中所说的指针。

而(Student)ht[id]返回值也是引用。

c#中Hashtable方法返回值的探索