Where are static members stored in memory? stack/ heap in C# .Net [duplicate]

旧时模样 提交于 2019-12-13 11:13:20

问题


The earlier post dealt with the value and reference types and their memory allocation.

Here I'm trying to understand the memory allocation of static members.

I have a simple class which has got both static and non-static integers like one shown below.

 class Sample
 {
   public int nonStaticInt = 0;
   public  static int staticInt = 0;
 }

My question here is, where do static integer reside? Stack/ a Heap. And how do they get into memory first even before any object creation.

Thanks!


回答1:


When a static variable is allocated, it will be stored as part of Methodtable. Methodtable means When a class is loaded first time in application, separate memory will be allocated in appdomain for class level variables and methods inside class. .

If static variable is primitive type, it will be stored as part of Methodtable. If it is reference type, it will be stored inside the heap and the reference will be stored in Methodtable



来源:https://stackoverflow.com/questions/35626252/where-are-static-members-stored-in-memory-stack-heap-in-c-sharp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!