const variables

  • Thread starter Thread starter ata
  • Start date Start date
A

ata

Hi,
consider the following class:

public class MyClass
{
public const String TableName = "myTable";

public const String Relation1 = TableName + "_WhateverEntity";
}

I would like to know whether the Relation1 constant calculated every
time the class is instantiated. Is the variable being initialized at
the compile-time or on the run-time?

Thanks.
 
Consts are, by definition, constant so it's the compiler that does the work
here.


--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
consider the following class:

public class MyClass
{
public const String TableName = "myTable";

public const String Relation1 = TableName + "_WhateverEntity";
}

I would like to know whether the Relation1 constant calculated every
time the class is instantiated. Is the variable being initialized at
the compile-time or on the run-time?

You can answer questions like this by means of the intermediate language
disassembler, ILDASM.EXE. Just write your code and compile it into an .exe.
Then open a Visual Studio command prompt and start ILDASM on your assembly:
ILDASM myprogram.exe. You will see a tree on screen where you class
contains, among other things, your "public static literal string" constants.
Double-click on Relation1 and you will see its corresponding MSIL code:

..field public static literal string Relation1 = "myTable_WhateverEntity"

Which shows you that the compiler has indeed performed the operation.
 
You can answer questions like this by means of the intermediate language
disassembler, ILDASM.EXE. Just write your code and compile it into an .exe.
Then open a Visual Studio command prompt and start ILDASM on your assembly:
ILDASM myprogram.exe. You will see a tree on screen where you class
contains, among other things, your "public static literal string" constants.
Double-click on Relation1 and you will see its corresponding MSIL code:

.field public static literal string Relation1 = "myTable_WhateverEntity"

Which shows you that the compiler has indeed performed the operation.

Thanks. Does the same concept apply to the C++ programs?
 
Peter said:
While the conclusion is correct, the logic used to arrive at it is
false. Not all implementations of a "constant" are compile-time. Read-only
variables with initializers are constant too, but those are
run-time constructs. Being "constant" does not actually imply
compile-time initialization.

Actually in C# it does. A read-only variable would be marked "readonly"
("initonly" in MSIL), not "const".

All "const" variables are substituted by the compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top