JustGreg
JustGreg

Reputation: 241

Yet another reference/value type misuse

It looks like I didn't manage to understand the concept behind reference and value type lists.

Here, I want to fill up an array of 30000 uint16s, with 125 values at a time.

When finished I want to add it to a list and start to fill up another chunk of 30000 values. However, I only store reference(s) to one array. Here is a test code:

UInt16[] ND = new UInt16[30000];
OMD.Clear();
for (int i = 0; i < 30000; i++) ND[i] = (ushort)i;
OMD.Add(ND);
for (int i = 0; i < 30000; i++) ND[i] = 13;
OMD.Add(ND);

In the second loop the first array member of OMD loses its values and changes to 13.

Upvotes: 0

Views: 94

Answers (1)

McGarnagle
McGarnagle

Reputation: 102793

You could clear the reference and create a new one after each cycle.

UInt16[] ND = new UInt16[30000];
OMD.Clear();
for (int i = 0; i < 30000; i++) ND[i] = (ushort)i;
OMD.Add(ND);

ND = new UInt16[30000];
for (int i = 0; i < 30000; i++) ND[i] = 13;
OMD.Add(ND);

Upvotes: 1

Related Questions