Memory allocation for const in C#

后端 未结 1 431
逝去的感伤
逝去的感伤 2021-01-17 13:34

How is memory allocated when I use:

public class MyClass
{       
    public const string myEVENT = \"Event\";
    //Other code
}
1条回答
  •  伪装坚强ぢ
    2021-01-17 13:56

    Well, it's a compile-time constant - so if you use it from other assemblies, "Event" will be copied into the IL for those other assemblies. Whether that gets interned cross-assembly or not depends on a CLR setting IIRC.

    However, if you're worried about whether you'll get a new string or a new string variable for each instance of MyClass, you don't need to worry - const implies static.

    In short, unless you've got huge, huge wads of constants (or enormous string constants) it's not going to cause you an issue.

    0 讨论(0)
提交回复
热议问题