码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2014-10-20 11:19:25      阅读:237      评论:0      收藏:0      [点我收藏+]

标签: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方法返回值的探索

标签:style   blog   color   io   ar   for   sp   div   on   

原文地址:http://www.cnblogs.com/qiangua/p/4036620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!