静态,常量和只读字段在哪里分配内存? [英] Where does the memory is allocated for static,constant and readonly fields?

查看:46
本文介绍了静态,常量和只读字段在哪里分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在程序中使用了三个字段,并在用法上有所不同,但是我对这些字段存储在何处感到困惑不大?在数据段(堆栈还是堆?)或代码段中?

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;

ILDASM中的字段描述如下

in ILDASM the the fields are described as the following

对于静态:.field private静态int32 a

for static: .field private static int32 a

对于常量:.field私有静态文字int32 b = int32(0x000004D3)

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

对于只读:.field private initonly int32 c

for readonly: .field private initonly int32 c

推荐答案

您知道const是静态的,这意味着它存储在堆中.只读就像一个成员.与其他成员一样,readonly的值也存储在堆中.有关const和readonly的更多参考,请参见下面的链接. https://blogs.msdn.microsoft.com/csharpfaq/2004/12/03/what-is-the-difference-between-const-and-static-readonly/

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/

这篇关于静态,常量和只读字段在哪里分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆