標籤:style blog color io ar for sp div on
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方法傳回值的探索