首页 > 代码库 > 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方法返回值的探索
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。