Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would this compile?

Tags:

c#

constants

I created a "const" for a value previously explicitly stated several times in my code:

private static readonly int QUARTER_HOUR_COUNT = 96;

When I did a search-and-replace of 96 for QUARTER_HOUR_COUNT, I inadvertently also replaced the declaration, so it became:

private static readonly int QUARTER_HOUR_COUNT = QUARTER_HOUR_COUNT;

...yet it compiled. I would think that it would disallow that. Why was it accepted by the compiler as a valid declaration?

like image 358
B. Clay Shannon-B. Crow Raven Avatar asked Aug 15 '12 16:08

B. Clay Shannon-B. Crow Raven


People also ask

Why would a program be compiled?

Compiling allows the computer to run and understand the program without the need of the programming software used to create it. When a program is compiled it is often compiled for a specific platform (e.g., IBM platform) that works with IBM compatible computers, but not other platforms (e.g., Apple platform).

What does it mean to compile a program?

Compile refers to the act of converting programs written in high level programming language, which is understandable and written by humans, into a low level binary language understood only by the computer.

What does it mean to compile a file?

Compiling is the transformation from Source Code (human readable) into machine code (computer executable). A compiler is a program.

Why do we compile a program in Java?

Java code needs to be compiled twice in order to be executed: Java programs need to be compiled to bytecode. When the bytecode is run, it needs to be converted to machine code.


2 Answers

I would think that it would disallow that. Why was it accepted by the compiler as a valid declaration?

Presumably because the language specification allows it. Do you have a specific rule in the language specification which you think prohibits it?

If your question is really "why doesn't the language specification prohibit this" - I suspect it's because it's probably quite hard to make sure you only prohibit things you really want to prohibit, while actually prohibit all such things.

You could argue that for simple cases of assignment directly back to itself, it would be good to have a special case in the language spec, but it would introduce complexity into the language for relatively little benefit.

Note that even if you didn't get an error, I'd expect you to get a warning - something like this:

Test.cs(3,33): warning CS1717: Assignment made to same variable; did you mean to assign something else?

Also note that if you make it a const instead of just a static readonly variable, then you do get a compile-time error:

Test.cs(3,23): error CS0110: The evaluation of the constant value for 'Program.QUARTER_HOUR_COUNT' involves a circular definition

Also note that by .NET naming conventions, this ought to be called QuarterHourCount, rather than having a SHOUTY_NAME.

like image 198
Jon Skeet Avatar answered Sep 24 '22 21:09

Jon Skeet


The IL code generated by the code is this:

 IL_0007:  ldsfld     int32 Example.Quat::QUARTER_HOUR_COUNT//Load the value of a static field on the stack
 IL_000c:  stsfld     int32 Example.Quat::QUARTER_HOUR_COUNT// Store the value from the stack in the static field

Since the default value of QUARTER_HOUR_COUNT is 0,the 0 is assigned to QUARTER_HOUR_COUNT

like image 6
Anirudha Avatar answered Sep 21 '22 21:09

Anirudha