关于C#中的ArrayList

最近刚刚开始使用C#来编写程序,由于在C++中使用习惯了Vector,通过简单查找MSDN发现可以用
C#中的ArrayList来代替Vector,就决定使用ArrayList.等程序写好以调试发现程序的结果和自己预想的不对,通过调试对ArrayList得出了下列的结论。

 

当使用Add方法添加基本类型变量成员时,ArrayList中保存的是基本成员的实际值,我称之为值保存。

当用用Add方法添加类对象变量成员时,ArrayList中保存的是类对象的引用,其实就是c/c++中的指针,我称之为引用保存。

因此如果使用ClassAAA/int aaa=(ClassAAA/int) ArrayVariableBB[i];得到的了变量 aaa,然后对aaa进行修改,将由也因aaa中保留的类型有不同的效果。可以通过下列的程序,更深入的理解这一点。

 

 

 

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

public class testclass
{
public    String str1;
public    String str2;
public    String str3;
    public testclass(int i)
    {
       
        str1 = ((int)(i++ +1)).ToString();
        str2 = ((int)(i++ + 1)).ToString() ;
        str3 = ((int)(i++ + 1)).ToString();
    }
}

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList aaa = new ArrayList();
            ArrayList aaa2 = new ArrayList();

            int a = 2;
            aaa.Add(a);
            a=3;
            aaa.Add(3);

            int b;
            b = (int)aaa[0];
            b = 100;     //aaa[0]的值还是2.

            testclass tc = new testclass(1);

            aaa2.Add(new testclass(100));
            aaa2.Add(new testclass(200));
            tc = (testclass)aaa2[0];
            tc.str1 = "aaa";//aaa2[0].str1 也变为"aaa"了。

        }
    }
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值