List C# capacity is always 0 even though using a constructor with capacity ? -
given simple synchronization code :
using system; using system.collections; using system.collections.generic; using system.linq; using system.text; using system.threading; using system.threading.tasks; namespace mutex { class calcarraythreads { private static system.threading.mutex mutex = new system.threading.mutex(); private const int threadsnumber = 10; private static list<int> list = new list<int>(10); public static void sumpartialarray(object index) { mutex.waitone(); int indexboxed = (int) index; int sum = 0; (int = indexboxed; < indexboxed + 9; i++) { sum += i; } console.writeline(string.format("thread : {0} calculated value : {1}", thread.currentthread.name, sum)); // list.add(sum); list[indexboxed] = sum; mutex.releasemutex(); // console.writeline(list.count()); } static void main(string[] args) { (int = 0; < threadsnumber; i++) { thread mythread = new thread(new parameterizedthreadstart(sumpartialarray)); mythread.name = string.format("thread{0}", + 1); mythread.start(i); } console.read(); } } }
when use line :
list[indexboxed] = sum;
i :
unhandled exception: system.argumentoutofrangeexception: index out of range. must non-negative , less size of collection.
even though capacity of list 10 .
why ?
if did following array, it'd work expected, setting second element:
int[] list = new int[10]; list[2] = 5; // second element 5, no exception thrown
looking @ list<t>
constructor, when pass in capacity, it's doing similar using internal array:
this._items = new t[capacity];
so seems should work then, when try access element less capacity, here's how indexer implemented:
public t this[int index] { { if (index >= this._size) throw new argumentoutofrangeexception("..."); return this._items[index]; } set { if (index >= this._size) throw new argumentoutofrangeexception("..."); this._items[index] = value; } }
it's checking _size
variable first, , throwing exception if index
you're requesting larger it. if weren't check, it'd work expect.
_size
initialized 0 unless pass non-empty collection constructor, , changes value when use add
, removeat
, clear
, etc methods. internally, it's still using array store elements, , if _size
greater capacity (say, after trying add
1 more element), allocates new array , copies elements older (smaller) array it.
i see 2 solutions consider using:
either use array, this:
private static int[] list = new int[10];
or supply collection of default value (here, bunch of zeroes) via constructor:
private static list<int> list = new list<int>(enumerable.repeat(0, 10));
Comments
Post a Comment