Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where Are Value Types Stored In (C#) Generic Collections

It is true that generic collections perform better than non-generic collections for value types. (i.e. List vs. ArrayList).

But why is that, other than the boxing-unboxing step? Where are the value type objects stored once added to the collection? In non-generic collections, they'll be boxed and stored on heap, what is different in generics?

like image 261
Curiouser Avatar asked Sep 24 '10 19:09

Curiouser


1 Answers

In generics, such as List<T>, they're still stored on the heap. The difference is that, internally, a List<int> makes a single array of integers, and can store the numbers directly. WIth ArrayList, you end up storing an array of references to boxed integer values.

like image 83
Reed Copsey Avatar answered Oct 16 '22 04:10

Reed Copsey