Click here to Skip to main content
15,892,809 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to organize this data Pin
BillWoodruff29-Dec-14 9:20
professionalBillWoodruff29-Dec-14 9:20 
GeneralRe: How to organize this data Pin
PIEBALDconsult29-Dec-14 10:04
mvePIEBALDconsult29-Dec-14 10:04 
GeneralRe: How to organize this data Pin
BillWoodruff29-Dec-14 12:12
professionalBillWoodruff29-Dec-14 12:12 
GeneralRe: How to organize this data Pin
SledgeHammer0129-Dec-14 10:50
SledgeHammer0129-Dec-14 10:50 
GeneralRe: How to organize this data Pin
BillWoodruff29-Dec-14 12:11
professionalBillWoodruff29-Dec-14 12:11 
GeneralRe: How to organize this data Pin
SledgeHammer0129-Dec-14 12:28
SledgeHammer0129-Dec-14 12:28 
GeneralRe: How to organize this data Pin
BillWoodruff29-Dec-14 14:18
professionalBillWoodruff29-Dec-14 14:18 
GeneralRe: How to organize this data Pin
SledgeHammer0129-Dec-14 14:58
SledgeHammer0129-Dec-14 14:58 
Sadly, it is you who does not understand LINQ fully. Allow me to impart some wisdom on you with my mighty hammer Smile | :) .

List<int> lst = new List<int>();

lst.Add(1);
lst.Add(2);
lst.Add(3);

System.Diagnostics.Debug.WriteLine(lst[1]);

var v1 = lst.OrderBy(x => x);

var v2 = lst.ToList();

lst[1] = 7;

foreach (int i in lst)
    System.Diagnostics.Debug.WriteLine("L: " + i);

foreach (int i in v1)
    System.Diagnostics.Debug.WriteLine("V1: " + i);

foreach (int i in v2)
    System.Diagnostics.Debug.WriteLine("V2: " + i);


Output:

2
L: 1
L: 7
L: 3
V1: 1
V1: 3
V1: 7
V2: 1
V2: 2
V2: 3

You'll notice that the original list has become 1,7,3 (the L). V1 has become 1,3,7 because its the same list (and sorting is lazy). Now notice V2... OMG... its still 1,2,3!!!

Crazy, huh?

ToList() and ToArray() are special in LINQ. They make a COPY of the list. However, it is a SHALLOW copy. Since ints, floats, doubles, etc. are VALUE types, you get a brand new copy of them.

With REFERENCE types, you'll still get a BRAND NEW list with references to the original objects. HOWEVER, if you add / remove from the original list, the add / delete will not be propagated because its a different list.

If you didn't do the ToList(), it would work the way you expect with reference types too.

With that being said, most other LINQ operations are lazy. However, ToList() and ToArray() are NOT.

Thank you. Please play again Smile | :) .
GeneralRe: How to organize this data Pin
BillWoodruff29-Dec-14 23:44
professionalBillWoodruff29-Dec-14 23:44 
GeneralRe: How to organize this data Pin
SledgeHammer0130-Dec-14 4:13
SledgeHammer0130-Dec-14 4:13 
GeneralRe: How to organize this data Pin
PIEBALDconsult29-Dec-14 13:07
mvePIEBALDconsult29-Dec-14 13:07 
GeneralRe: How to organize this data Pin
BillWoodruff29-Dec-14 23:15
professionalBillWoodruff29-Dec-14 23:15 
GeneralRe: How to organize this data Pin
SledgeHammer0130-Dec-14 4:17
SledgeHammer0130-Dec-14 4:17 
GeneralRe: How to organize this data Pin
BillWoodruff30-Dec-14 23:42
professionalBillWoodruff30-Dec-14 23:42 
GeneralRe: How to organize this data Pin
PIEBALDconsult30-Dec-14 15:29
mvePIEBALDconsult30-Dec-14 15:29 
GeneralRe: How to organize this data Pin
BillWoodruff30-Dec-14 23:53
professionalBillWoodruff30-Dec-14 23:53 
GeneralMessage Closed Pin
29-Dec-14 2:32
Marvic Grima29-Dec-14 2:32 
Questionserial port CreateFile in c# Pin
Member 1114321929-Dec-14 2:16
Member 1114321929-Dec-14 2:16 
AnswerRe: serial port CreateFile in c# Pin
OriginalGriff29-Dec-14 2:28
mveOriginalGriff29-Dec-14 2:28 
GeneralRe: serial port CreateFile in c# Pin
Member 1114321929-Dec-14 2:45
Member 1114321929-Dec-14 2:45 
GeneralRe: serial port CreateFile in c# Pin
Member 1114321929-Dec-14 2:55
Member 1114321929-Dec-14 2:55 
GeneralRe: serial port CreateFile in c# Pin
OriginalGriff29-Dec-14 3:42
mveOriginalGriff29-Dec-14 3:42 
Questionusing SqlConncetion Pin
Jassim Rahma29-Dec-14 1:16
Jassim Rahma29-Dec-14 1:16 
AnswerRe: using SqlConncetion Pin
OriginalGriff29-Dec-14 2:02
mveOriginalGriff29-Dec-14 2:02 
AnswerRe: using SqlConncetion Pin
Dominic Burford30-Dec-14 5:52
professionalDominic Burford30-Dec-14 5:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.