zl程序教程

您现在的位置是:首页 >  后端

当前栏目

c#中结构体和类在队列中的引用类型问题

c#队列队列 类型 结构 引用 问题
2023-09-11 14:16:45 时间

 class test
    {
        public  int a;
    }

 struct test
    {
        public  int a;
    }

void test()

{

List <test > tt= new List<test>();
            test aa = new test();
            aa.a = 99;
            tt.Add(aa);
            test cc = tt.First();
            cc.a = 88;

}

这个例子中test如果是类,则cc的值改变可以改变tt中列表的内容,如果是结构体则不能,说明类是引用类型从堆中获取了内存地址给cc,而结构体则是拷贝了tt中的内容给cc在栈上。