DEV Community

loading...

Discussion on: Interview question: heap vs stack (C#)

akashkava profile image
Akash Kava • Edited

Type is something you can always do typeof(x), you can never do type of (field of (class/struct)). Example,

    struct A {
       ? a;
    }
Enter fullscreen mode Exit fullscreen mode

First of all you can never do typeof(A.a) because a is a field of type, it is not a type !

FieldInfo.FieldType is type of field, field is not type. Again, value type is a type, which you can safely do typeof(int), typeof(string), anything that can sit inside typeof expression is a type, field is not type.

Here, A is a type, since it is a struct, it will always be on stack unless captured by lambda. And whatever may be the type field a, A will always be on stack !! Member of a type is not type !! Field/Method/Property all are member of type and allocation will never depend on them. If a is string, it is reference, but string is a type, a is not type, and A will still sit on stack and contents of string will be on heap and a will store reference and entire object will sit on stack.

Thread Thread
tyrrrz profile image
Alexey Golub Author

You can do A.a.GetType() to get the field type. Field type can be value type. I'm not talking about field being a type.

Forem Open with the Forem app