Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does compile time 'const' mean?

Tags:

c#

They say the difference between readonly and const is that const is compile-time (while readonly is run time). But what exactly does that mean, The fact that it's compile time? Everything gets compiled to byte code doesn't it?

like image 670
Shai UI Avatar asked Dec 19 '10 23:12

Shai UI


People also ask

What are compile-time constants in Java?

4.1. Compile-Time Constants. A Java variable is a compile-time constant if it's of a primitive type or String, declared final, initialized within its declaration, and with a constant expression. Strings are a special case on top of the primitive types because they are immutable and live in a String pool.

What is compile-time constant in Dart?

A variable that is declared using the const keyword cannot be assigned any other value. Also, the variable is known as a compile-time constant, which in turn means that its value must be declared while compiling the program.

What's the difference between compile-time and runtime?

Compile time is the period when the programming code (such as C#, Java, C, Python) is converted to the machine code (i.e. binary code). Runtime is the period of time when a program is running and generally occurs after compile time.

What is compile-time and runtime C++?

Compile-time and Runtime are the two programming terms used in the software development. Compile-time is the time at which the source code is converted into an executable code while the run time is the time at which the executable code is started running.


1 Answers

It means that const variables get written to the location they are referenced from. So, say you have an 2 libraries, one with a const variable:

// Library A
const int TEST = 1;

// Library B
void m ()
{
   Console.WriteLine(A.TEST);
}

The variable is actually written, at compile time, into B. The difference is, if you recompile A but not B, B will have the "old" value. This won't happen with readonly variables.

like image 121
Noon Silk Avatar answered Oct 15 '22 06:10

Noon Silk