Structs, heaps, and stacks
I've been reading a book about C# and came across the topic of storing values in memory. An instance of a reference type is always created on the heap, however a variable's value lives wherever it's declared. Only local variables (variables declared within methods [not anonymous]) and method parameters live on the stack.
So my question is - if I declare those structs as such local variables - would they be all put on the stack?
struct A<T> where T : struct { }
struct B<T> where T : class { }
struct C { }
I am simply wondering if the content of a struct
can have any influence on where it will be stored in the memory.
Thanks, C# gurus!
structs go where you tell them to go.
If they are declared as a local variable in a function then they are on the stack. If they are class members then they are inline in the heap memory of the class.
If a struct contains a class then the reference is inlined in the struct just like it was an int or any other member. The thing the class reference points at is on the heap
链接地址: http://www.djcxy.com/p/79090.html上一篇: .NET垃圾收集器基础
下一篇: 结构,堆和堆栈