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

來源:互聯網
上載者:User

標籤: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方法傳回值的探索

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.