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

后端 未结 5 1739
北海茫月
北海茫月 2021-02-08 09:31

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-unboxi

5条回答
  •  迷失自我
    2021-02-08 10:15

    An ArrayList is an local array of references to objects stored in the heap.

    A generic List of references types is a local array of references to objects stored in the heap.

    A generic List of value types is a local array of those value types.

    There are two areas of memory, which most references call "The Stack" and "The Heap". Most people who use those terms have no idea why. ("The Stack" may be a stack, but The Heap almost certainly isn't a heap). I prefer the terms "Over Here" and "Over There". When boxed, the value type data is stored "Over There". When stored in an array (perhaps inside a generic List), the value type data is stored "Over Here". "Over here" is better.

提交回复
热议问题