Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
a constant is a “value” (specifically a literal value such as int type literal value 4 or string type literal value “Hello”). Thus, “constant” does not refer to (is not the same as) a field or local variable declared with the const modifier. But is a value (an immutable literal value) that can be assigned to either a field or local variable declared or not declared with const modifier. Thus, a (literal) value is still a constant even if it is not assigned to a field or local variable declared with the const modifier or if not assigned to anything at all. And as we know a literal value is immutable thus, it is the same as to say that a constant is immutable.

A field declared with the const modifier is referred to as a “constant field” which must be assigned with a constant (since the const modifier enforces that) upon declaration of the constant field. Also, a constant field cannot be assigned with a new value after its declared (since the const modifier enforces that as well).

A local variable declared with the const modifier is referred to as a “constant local” which also must be assigned with a constant (since the const modifier enforces that) upon declaration of the constant field. Also, a constant local cannot be assigned with a new value after its declared (since the const modifier enforces that as well).

Therefore, "constant" and "constant field" and "constant local" all mean different things.

My prior knowledge on these topics were so far off and incorrect. But this is now my current understanding on the matter. Please correct me if anything I said was incorrect. Many thanks.

What I have tried:

I'm trying to figure out if my current understanding is correct.
Posted
Comments
Richard MacCutchan 8-Aug-21 4:12am    
Yes, it is correct.
OriginalGriff 8-Aug-21 4:23am    
Jake's back, I see.
Richard MacCutchan 8-Aug-21 4:27am    
I thought so too, but ...
OriginalGriff 8-Aug-21 4:54am    
As long as he's learned to behave, it's not a problem.
But from his history, I suspect he'll become abuse pretty quickly ... :sigh:
Richard MacCutchan 8-Aug-21 5:43am    
Agree.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900