c# - Why are member constants available even if there are no instances of a its class? -


1) Why not even the member class is still stable?

2) Is the only reason why the compiler needs constant evaluation in time compilation fully, while the variable variables have been replaced with literal value?

3) Since the string is also an object, I would think that an error will occur in the following, but it is not. Why?

  class A {const string b = "this works"; }  

thanks

  1. constant ( Declared by const ) - so there is no need for an example.

  2. A const value is embedded in assembly, it has been declared, and then it is used every time , That value is also copied to the calling code, so its execution can not be evaluated on time - if you want that behavior, then use to read steady .

  3. String literals have constant values ​​C # language specification is basically a metadata representation for wire in IL, so that they can be specified as constants. String constants have other interesting properties such as interning .

One point of interest: You can declare the code as decimal as the field in const in C # But this actually does not have CLR support ... there is no real form C # compiler makes it by using the [decimalconstant] attribute. Can not use the decimal argument as an attribute logic type.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -