November 2019
Intermediate to advanced
404 pages
12h 9m
English
The purpose of arrays is to contain large datasets, which makes them difficult to be treated as a value type since there's probably not enough room on the stack to support them. Therefore, they are treated as a reference type so that the entire dataset can be passed around via a single reference (if it were a value type, we would need to duplicate the entire array every time it is passed around). This is true irrespective of whether the array contains value types or reference types.
This means that the following code will result in a heap allocation:
TestStruct[] dataObj = new TestStruct[1000];for(int i = 0; i < 1000; ++i) { dataObj[i].data = i; DoSomething(dataObj[i]);}
However, the following, functionally equivalent, ...
Read now
Unlock full access