17

How is memory allocated when I use:

public class MyClass
{       
    public const string myEVENT = "Event";
    //Other code
}
softwarematter
  • 28,015
  • 64
  • 169
  • 263

1 Answers1

16

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    I think this wikipedia page for [Oxymoron](http://en.wikipedia.org/wiki/Oxymoron) should point back here... (Jon using IIRC ...it's like Chuck Norris wondering if he can get out of bed. Look [here](http://meta.stackexchange.com/a/9182/237379) for other Jon facts ... :) – Noctis May 07 '14 at 23:42
  • Hey @Jon Skeet , - "const implies static. " means conatant is by default static and single copy of constant variable will be generated for different all objects. ? –  Dec 10 '17 at 04:17
  • 1
    @vimalraturi: It means there's a single field regardless of the number of instances, even if there are none of them. – Jon Skeet Dec 10 '17 at 09:13
  • 1
    Saying it won't cause an issue unless you have huge numbers of constants is incorrect. As a constant is replaced at compile time, it should never have an issue. https://stackoverflow.com/questions/23529592/how-are-c-sharp-const-members-allocated-in-memory – CBFT May 19 '22 at 19:52
  • @CBFT: The fact that it's replaced at compile time *is the point*. You end up with a copy of the constant in every assembly that references that constant. As I say in the answer, whether those separate copies (one per assembly) are then interned is a more complicated matter. – Jon Skeet May 20 '22 at 05:57
  • Saying it acts the same as the literal "AConstant" would be more accurate than a static variable. A static variable cannot be garbage collected. I looked at your answer and thought I should avoid const given how large our application is. – CBFT May 20 '22 at 14:21
  • @CBFT: Given that I specifically say "unless you've got huge, *huge* wads of constants it's not going to cause you an issue" I think that's an overreaction. Note that I didn't say it acts the same as a static variable - I said that `const` implies `static`, which it does - and the context of the rest of the sentence was in comparison with an instance variable. (Not that constants can be garbage collected either, as far as I'm aware.) – Jon Skeet May 20 '22 at 14:37
  • @CBFT: Of course, if you *really* think this 12-year-old question should be answered differently, there's nothing to stop you from adding your own answer. I'm content with the one I've written. – Jon Skeet May 20 '22 at 14:39
  • I and many users are using these answers to write code currently. That is why having misleading answers to old questions is still a problem. There are very good answers already here: https://stackoverflow.com/questions/23529592/how-are-c-sharp-const-members-allocated-in-memory – CBFT May 21 '22 at 12:36
  • @CBFT: Again, I don't believe this is misleading. This is my last comment here - I don't think it's productive to discuss this any further. – Jon Skeet May 21 '22 at 14:33