gpt4 book ai didi

c# - 静态、常量和只读字段的内存分配在哪里?

转载 作者:太空狗 更新时间:2023-10-30 00:37:16 27 4
gpt4 key购买 nike

我已经在程序中使用了这三个字段并了解了用法上的差异,但我有点困惑这些字段存储在哪里?在数据段(堆栈或堆?)或代码段?

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

在ILDASM中,字段描述如下

对于静态:.field private static int32 a

常量:.field private static literal int32 b = int32(0x000004D3)

对于只读:.field private initonly int32 c

最佳答案

如您所知,const 是静态的,这意味着它存储在堆中。 Readonly 就像一个成员。就像任何其他成员一样,readonly 的值也存储在堆中。有关 const 和 readonly 的任何进一步引用,请引用下面的链接。 https://blogs.msdn.microsoft.com/csharpfaq/2004/12/03/what-is-the-difference-between-const-and-static-readonly/

关于c# - 静态、常量和只读字段的内存分配在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54105028/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com