我有这样一个属性类using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace TCPServer
{
    public class ClientInfo
    {
        public int id;
        public int ip1;
        public int ip2;
        public int ip3;
        public int ip4;
        public int port;
        public int ListBoxTop;//ListBox索引
        public int TcpGetLength;//发送给客户端的字节数
        public int TcpSetLength;//接收到客户端得字节数
        public int StationID=-1;          //数据表 PYH_WaterLevel 中表示节点编号字段
        public string dataTime;
        public int GprsID;//对应数据库中的字段,模块编号
        //构造函数
        public ClientInfo(int ip1, int ip2, int ip3, int ip4, int id, int port,int GprsID,int StationID)
        {
            this.ip1 = ip1;
            this.ip2 = ip2;
            this.ip3 = ip3;
            this.ip4 = ip4;
            this.id = id;
            this.port = port;
            this.TcpGetLength = 0;
            this.TcpSetLength = 8;
            this.StationID = StationID;
            this.GprsID = GprsID;
            this.dataTime = DateTime.Now.ToString();
        }        public string GetIp()
        {
            return ip1 + "." + ip2 + "." + ip3 + "." + ip4;
        }
    }
}我的调用方法如下   ClientInfo[] clienInfo = new ClientInfo[500];
   tcpServer.getClientInfo(ref clienInfo);//这个方法会将  clienInfo  赋值
    //  假设 id=2  i =1 @@@@
   int listInsex = clienInfo[id].ListBoxTop;
   int GprsAddress = clienInfo[id].StationID;
   clienInfo[id] = clienInfo[i];
                        
   if (listInsex != 0)
   {
     clienInfo[id].ListBoxTop = listInsex - 1;
    }
    clienInfo[id].id = id;
    clienInfo[id].StationID = GprsAddress;
 问题是我给   clienInfo[id].id = id;   的时候  clienInfo[i].id 的值也跟着   改变了  不仅如此, 其它的参数也是一样的跟着改变。
请问各位, 是我的理解有问题还是  可能我电脑中毒了呢? 听说最近流行一种专门针对程序员的病毒。 我是不是中招了?我这里只是 赋值 覆盖的问题啊。  貌似没有牵扯到指针。 。

解决方案 »

  1.   

    clientInfo[id] = clientInfo[i];
    这一句使得 clientInfo[id] 和 clientInfo[i] 都引用同一个 ClientInfo 实例了。后面在改变其中一个的属性,另一个当然也跟着改变了。
      

  2.   

    类是引用类型的,clientInfo[id] = clientInfo[i]; 这样做的时候,就指向了同一个实例了
      

  3.   

    事实上,这两个引用指向的是同一个实例
    clienInfo[id] = clienInfo[i];当你通过一个引用修改实例时,另一个引用看到的实例自然也改了。
      

  4.   

    clienInfo[id] = clienInfo[i];
    这一行错了,如果想把值付过去好像有copy之类的东西