结构啊,有木有

居然自己被 值类型和引用类型 弄混了 哎

public class Temp
{
public string temp;
}
public struct cycle
{
public ServiceType servicetype;
public TimeEnum timeenum;
public DateTime time;
public TimeSpan sleeptime;
}

public void test(){

      List
<cycle> list1 = new List<cycle>();
cycle tt1
= new cycle();
tt1.servicetype
= ServiceType.Consult;
list1.Add(tt1);
cycle tt2
= list1[0];
tt2.servicetype
= ServiceType.CRM;
//List1[0] servicetype为Consult

List
<Temp> list = new List<Temp>();
Temp t1
= new Temp();
t1.temp
= "10";
list.Add(t1);
Temp t2
= list[0];
t2.temp
= "20";
//list[0] temp值为 20
}
记住一句话:struct text是一个结构。
List1[0]返回了结构的一个拷贝,在拷贝上赋值是没有意义的。

原文地址:https://www.cnblogs.com/shikyoh/p/2079950.html