9

Why does the compiler store constant values in the assembly metadata? Are they directly embedded to Intermediate Language code from the assembly metadata?

Avner Shahar-Kashtan
  • 9,166
  • 3
  • 29
  • 37
Arun
  • 477
  • 1
  • 3
  • 14
  • 1
    this doesn't really address the 'why' but is interesting nonetheless: http://stackoverflow.com/a/2128633/128384 – stijn Jan 02 '13 at 13:35
  • I am confused by this question. **Where else would they be stored** if not in metadata? Can you clarify the question? – Eric Lippert Jan 04 '13 at 16:41

4 Answers4

4

The best way to think about it I find is: At compile time a const is converted to a literal where it is used.

The only reason it goes into the manifest in the assembly it was defined in was to make it accessible to consumers. It is a part of a given type and that type's metadata is stored in it's assembly, not the assemblies that consume it.

So, it is an in-line literal in consumption, and a consumable type-encapsulated property or field in metadata.

Jimmy Hoffa
  • 16,039
  • 3
  • 69
  • 80
2

I researched myself for quite long time and found from a book that this can be the reason...

Since constant values never changes, constants are considered to be part of the defining type. Therefore defining constants creates metadata.

Arun
  • 477
  • 1
  • 3
  • 14
1

I think you mix up two assemblies.

The constant is only stored in the metadata in the assembly where it is defined. Metadata contains information about all the types and members in an assembly and constants are members.

No information about the constant is stored in the assembly where it is used. The constant value is used directly in the IL just as if you had written the constant number or string directly in the source.

An example: One of my applications use a lot of constants to identify database tables and fields. I have one assembly that only contain all constants.

When I build the application I add a reference to the "constants" assembly in Visual Studio. Since the assembly only contain constants there is no reference to it in my application and the "constants" assembly is not needed at run-time.

adrianm
  • 196
  • 2
  • 2
    This doesn't really explain why constants are stored in the metadata, though. It dances around why, but I don't think the OP is confusing two assemblies at all. You see it as a two-assembly issue because you use two assemblies, but I don't think that practice is common; constants are typically defined in the same assembly where they are used. – Robert Harvey Jan 02 '13 at 16:42
  • @RobertHarvey Your presumption of the typical case overestimates the skill of many of our enterprisey C# colleagues, I have many times had to explain what a const is to colleagues and why you don't call them across assemblies. – Jimmy Hoffa Jan 03 '13 at 15:12
1

Constants are known at compile time and then they are stored in assembly's metadata. This means that you can only define constants for primitive types.

Yes they are directly loaded from metadata. There would be no memory allocation at runtime.

Striker
  • 19
  • 2
  • 1
    Decimal is not a primitive type but you can make decimal constants. And you can make constants of any reference type whatsoever provided that the constant is null. – Eric Lippert Jan 04 '13 at 16:40