Can you have a class in a struct?

前端 未结 4 673
Happy的楠姐
Happy的楠姐 2020-12-15 03:41

Is it possible in C# to have a Struct with a member variable which is a Class type? If so, where does the information get stored, on the Stack, the Heap, or both?

4条回答
  •  清歌不尽
    2020-12-15 04:19

    It's probably not a recommended practice to do so: see http://msdn.microsoft.com/en-us/library/ms229017(VS.85).aspx

    Reference types are allocated on the heap, and memory management is handled by the garbage collector.

    Value types are allocated on the stack or inline and are deallocated when they go out of scope.

    In general, value types are cheaper to allocate and deallocate. However, if they are used in scenarios that require a significant amount of boxing and unboxing, they perform poorly as compared to reference types.

提交回复
热议问题