5

I have used the three fields in the program and got the difference in usage but I am little confused where does these fields are getting stored? either in data segment(stack or heap?) or code segment?

static int a;
const int b=1235;
readonly int c;

in ILDASM the the fields are described as the following

for static: .field private static int32 a

for constant: .field private static literal int32 b = int32(0x000004D3)

for readonly: .field private initonly int32 c

3 Answers3

2

Every static variable is stored on the heap, regardless of whether it's declared within a reference type or a value type. There is only one slot in total no matter how many instances are created. (There don't need to be any instances created for that one slot to exist though.) The details of exactly which heap the variables live on are complicated. more info you can find HERE

Leon Barkan
  • 2,676
  • 2
  • 19
  • 43
  • Thanks for the answer, can you explain about the memory allocation constant and readonly fields? –  Jan 09 '19 at 07:39
  • 1
    @gurunagsai you've obviously already checked the [constants are classified as static members](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#static-classes), so that part is clear and `readonly` does not change allocation - so that is also answered by this post for static readonly fields (assuming you already know how instance fields are stored) – Alexei Levenkov Jan 09 '19 at 07:51
  • @AlexeiLevenkov Thanks for the answer . –  Jan 09 '19 at 08:16
2

As you know const is static which means it is stored in the heap. Readonly is just like a member. Just like any other member the value of the readonly also gets stored on the heap. For any furthur reference about const and readonly refer the link below. https://blogs.msdn.microsoft.com/csharpfaq/2004/12/03/what-is-the-difference-between-const-and-static-readonly/

  • 1
    Thanks for the answer –  Jan 10 '19 at 11:13
  • One thing that I am personally curious about: Does "readonly" act similar like "private / public" etc? Is it just a help-thingy by the .NET Compiler? You can still modify private values from outside by reflection, even though it is declared private. Does the same go for "readonly"? – Jannik Jan 10 '19 at 11:20
  • @Jannik read only can be modified only at the runtime or by using a non-static constructor – Akarsha Rao Jan 10 '19 at 11:26
  • This is a bit misleading. Const fields are baked into the compiled code. It's misleading to say const members are stored in the heap; They're potentially only stored in the code page, and are generally inlined by the compiler. – Brian Jan 28 '19 at 22:32
0

CLR is the basic and Virtual Machine component of the .NET Framework. It is the run-time enviornment in the .NET Framework that runs the codes and helps in making the development process easier by providing the various services. The CLR divides the memory into three distinct regions: the stack, the heap, and the high frequency heap. Static objects need to survive a GC collection and are stored in the high frequency heap. Static and constants objects are stored in the loader heap as they exist in the memory throughout the lifetime of the application; they don't need to be garbage collected.

fasak123
  • 1
  • 3