last day (15 days later) » 

11:37 PM
32
A: C#.NET - Why do members of a static class need to be declared as static? Why isn't it just implicit?

Eric LippertI get asked questions like this all the time. Basically the question boils down to "when a fact about a declared member can be deduced by the compiler should the explicit declaration of that fact be (1) required, (2) optional, or (3) forbidden?" There's no one easy answer. Each one has to be tak...

 
@Caspar: what about "private"? Because it is the default, it's optional. And because the default is the most restrictive, it is harmless to accidentally leave it off; you can't expose more than you intend, only less.
@Richard: the para was not very clear. I've clarified it.
 
@richard I thought it would be constructive to point out that constants are not variables. So, they are not static. They are literal values replaced by a name for convenience(const is like a restrictive #define). The compiler replaces every reference to a const with the literal value. static, on the other hand, is a variable. So, declaring a literal value as a static is an error and forbidden.
 
@ThunderGr: Almost every statement you've made is wrong. The first statement is correct; constants are not variables because variables are storage locations that can change. The second statement is categorically wrong; constants declared at the class or struct level are static members of the type. The third statement is wrong; a constant initializer need not be a literal; it can any expression classified as a constant expression, not just a literal. You are correct that the compiler inserts the value of the constant at its use site.
@ThunderGr: However, this is an implementation detail. Your statement "static is a variable" is nonsensical; "static" is a modifier on a declaration. Constant fields are always static fields and therefore the language makes it illegal to redundantly specify the modifier. Your last sentence implies that the previous sentences give a justification for this fact, but since all those previous sentences are wrong, they do not. The justification is given by the article linked in my answer.
 
How can it be wrong, Eric? Any constant expression is a literal. There is no doubt about that. Anyway, I respect your knowledge and expertise, but you are arguing upon semantics here, when you say that my statement is wrong. constants at any level cannot be static members, because static members are things that are not constant, thus variable. It is not just redundant. It is plain wrong. static is a storage modifier for a variable, not a literal or constant expression. So, despite your claims, I do not see any part of my statements being wrong.
 
@ThunderGr: Well there is certainly no doubt that the definition you have in your head of "literal" is wrong. These are literal constant expressions: 123, "hello", 0.3. These are non-literal constant expressions: 2 + 2, System.Math.Pi. All literal constant expressions are constant expressions, but not all constant expressions are literal expressions.
@ThunderGr: I am somewhat confused by your statement "you are arguing upon semantics here when you say that my statements is wrong". That is correct. "Semantics" are the meaning of a statement; a semantic analysis of your statements shows that they are incorrect.
 
11:37 PM
2+2 is a literal, because it is 4. Thus a literal. Any constant expression is a literal, because its value is converted to a literal at compile time. I think it is very simple. All constant expressions are literal, or they would not be constant.
 
@ThunderGr: Your statement "constants cannot be static members because static members are things that are not constant, thus variable" is simply false. Constant fields are static members but not variables.
@ThunderGr: You are using "literal" as a synonym for "constant expression". That is incorrect. Your statement "any constant expression is a literal" is simply completely wrong. A literal expression is a constant expression whose value appears literally in the source code rather than being computed or looked up.
 
I understand the sentiment, but this is not an argument you make. Prove that a constant expression is not a literal? It is always evaluated to a literal at compile time.
OK
I really see no point to the insistence that a constant expression is not a literal.
Everything evaluated to a value at compile time is a literal for the compiler.
A const is a literal, always. There is no doubt about that. In all languages
When I say "to a value" I mean in such a way that the value is inserted in the code and not a memory location.
Thus, the value is not modifiable in the code and so, it has no storage
something with no storage, cannot have a storage modifier(like static).
It is simple, really.
 

last day (15 days later) »