您的位置:首页 > 编程语言 > C#

c#---List共享空间的情况

2016-03-26 16:39 316 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace rand
{
class Program
{
class E
{
public int a;
public int b;

public E(int aa, int bb)
{
a = aa;
b = bb;
}
}

static void Main(string[] args)
{
List<E> l = new List<E>();
E A = new E(1, 2);
l.Add(A);
List<E> ll = new List<E>(l);   //!!!!
ll[0].a = 4;                   //!!!!
Console.WriteLine(l[0].a);     //结果为4,而不是原来的1
Console.ReadLine();
}
}
}




static void Main(string[] args)
{
E A = new E(1, 2);
E D = A;                 //!!!!!
D.a = 4;                 //!!!!!
Console.WriteLine(A.a);  //结果为4,而不是原来的1
Console.ReadLine();
}




static void Main(string[] args)
{
List<E> l = new List<E>();
E A = new E(1, 2);
l.Add(A);
List<E> ll = new List<E>();
E C = new E(1, 1);      //!!!!!!
C = l[0];               //!!!!!!
C.a = 4;                //!!!!!!
ll.Add(C);
Console.WriteLine(l[0].a);  //结果为4,而不是原来的1
Console.ReadLine();
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: