memory allocation of value types and reference types in .net framework

后端 未结 5 1989
独厮守ぢ
独厮守ぢ 2020-11-29 10:05

Is there an advanced article which I can read that can explain how memory is allocated for different types (value and reference) in .net framework.

for example we kn

5条回答
  •  感动是毒
    2020-11-29 10:40

    It's more complicated than you might think. Even your claim that "value types are allocated on the stack" isn't correct. For example:

    class Foo
    {
        int x;
    }
    

    int is a value type, but the value for x will always be on the heap because it will be stored with the rest of the data for the instance of Foo which is a class.

    Additionally, captured variables for anonymous functions and iterator blocks make life trickier.

    I have an article about C# heap/stack memory you may find useful, but you might also want to read Eric Lippert's blog post on "The stack is an implementation detail". In particular, a future C# compiler could decide to store all of its local variables on the heap, using the stack just to hold a reference to an instance created at the start of the method... that wouldn't defy the C# spec at all.

提交回复
热议问题