Readonly & Const

xiaoxiao2021-03-06  100

C # introduced the ReadOnly modifier to represent a read-only domain, constly indicates that the usual is not constant. As the name suggests that the only way to write is not written, the constant constant cannot be modified, what is the difference between these two? The read-only domain can only assign a value during initialization-declaration initialization or constructor, and other places cannot perform assignment operations for read-only domains, otherwise the compiler will report an error. A read-only domain can be an instance domain or a static domain. The type of read-only domain can be any type of C # language. However, constant constants must be assigned to the declaration, and the compiler can calculate this determined value during compilation period. Const modified constants are static variables that cannot be obtained for objects. The type of Const modified value is also limited, it can only be one of the following types (or can be converted to the following type): Sbyte, Byte, Short, USHORT, INT, UINT, Long, Ulong, Char, Float, Double, Dou, DECIMAL , BOOL, STRING, ENUM type, or reference type. It is worth noting that the reference type here is, because all types of the string type are removed from the NULL value, they cannot calculate their exact values ​​during compiler, so we can declare that the reference type of Const can only be String or other reference type of NULL. Obviously when we declare a NULL constant, we have lost the meaning of the statement - this can also be said to be the embarrassment of C # design!

http://www.microsoft.com/china/msdn/archives/cornyfield/cornyfield7.asp

转载请注明原文地址:https://www.9cbs.com/read-104977.html

New Post(0)