Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question
1  
this doesn't really address the 'why' but is interesting nonetheless: stackoverflow.com/a/2128633/128384 –  stijn Jan 2 '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 4 '13 at 16:41
add comment

4 Answers

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
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 2 '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 3 '13 at 15:12
add comment

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.

share|improve this answer
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 4 '13 at 16:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.