classListtest { Public Static voidTest () {#regionValue typevarOlistval =Newlist<int>() { 1,2,3,4 }; varNlistval = olistval.where (p = p >2). ToList (); //because T is a value type (int type), the elements of the new list are assigned a memory address (stack memory) with the elements of the old list.//change the elements of a new list without affecting the old list elementsnlistval[0] =5; if(olistval[2] = = nlistval[0]) {Console.WriteLine ("value type--changed original value"); } Else{Console.WriteLine ("value type--unchanged original value"); } #endregion #regionReference typevarOlistref =NewList<student>() { NewStudent () {Id=1, Name="A" }, NewStudent () {Id=2, Name="B" }, NewStudent () {Id=3, Name="C" }, NewStudent () {Id=4, Name="D" }, }; varNlistref = Olistref.where (p = p.id >2). ToList (); //because T is a reference type (class type), the elements of the new list share a memory address (heap memory) with the elements of the old list//changes the elements of the new list, and actually modifies the old list elementsnlistref[0]. Name ="G"; if(olistref[2]. Name = = nlistref[0]. Name) {Console.WriteLine ("reference Type--changed original value"); } Else{Console.WriteLine ("reference Type--unchanged original value"); } #endregion } classStudent { Public intId {Get;Set; } Public stringName {Get;Set; } } }
C # ilist.tolist () changing element values will affect the value of the original list