Re: Advantage of const against static readonly?

From: cody (deutronium_at_gmx.de)
Date: 01/16/05


Date: Sun, 16 Jan 2005 14:09:35 +0100


>>You aren't worried about the memory consumption of a static readonly value
>>which exists exactly one time due to its static nature, are you :)
>
> Not really, no. I'm just pointing out one of the differences. But the
> effect could be quite real if you consider a large enum. If enums were
> built on readonly static fields instead of consts you could probably
> waste a few KB with "useless" static fields taking up memory.

But enums *are* stored somewhere in the assembly there are declared in, or
did I misunderstood you? An Application will stop working if you would
remove the definition of the enum from the library it is using.

>>I suspect that the JIT will generate both both const and static readonly
>>exactly the same code as I cannot imagine a reason why I would not be
>>possible to optimize the code this way.
>
> While it can optimize use of the field, it can't "optimize away" the
> storage location for the static field itself.

Maybe but is the point here? As I poined out earlier you aren't worried
about the memory consumption of a static readonly value which exists exactly
one time :)

But I have indeed found one reason why const can be better than static
readonly field: Accessing a static readonly value forces the static
constructor of that class to run whereas this is not the case with const
values.



Relevant Pages

  • [PATCH 8/10] Re: [2.6-BK-URL] NTFS: 2.1.19 sparse annotation, cleanups and a bugfix
    ... -static int ntfs_attr_find(const ATTR_TYPES type, const ntfschar *name, ... -typedef enum { ... +typedef le32 NTFS_RECORD_TYPE; ... These are the so far known MFT_RECORD_* flags which contain ...
    (Linux-Kernel)
  • Re: wanna be expert
    ... ...where SOME_OPTIONS is a const or enum, depending on type requirements. ... In reality a lot of programmers use literals instead of named constants ...
    (comp.programming)
  • Re: String or Numeric Variable?
    ... it appears that using numeric constants will solve the 'readability' ... >>issue and Enum perhaps more efficient? ... > Const Afternoon As Long = 2 ... > Dim TimeOfDay as TOD ...
    (comp.lang.basic.visual.misc)
  • Re: String or Numeric Variable?
    ... it appears that using numeric constants will solve the 'readability' ... >issue and Enum perhaps more efficient? ... Const Afternoon As Long = 2 ... Dim TimeOfDay as TOD ...
    (comp.lang.basic.visual.misc)
  • Re: com-enum
    ... You need to use the wsf-file format and import the enum from ... the typelib either by a reference-tag or a reference-attribute ... there is direct way for typelib import here. ... Const LineStyles_Strichpunkt = 1 ...
    (microsoft.public.scripting.vbscript)

Loading