Const and readonly

Source: Internet
Author: User

Const: the amount of data that is often compiled, that is, the data is determined during compilation.

Readyonly: the number of runtime times, that is, when the runtime is computed, the compiled Il will maintain reference to the variable, rather than its worthy reference, similarC Language.

The compile-time constant can only be used for primitive and enumeration types, because only these types allow us to specify meaningful constant values during initialization.

What is a primitive type?The data types directly supported by the compiler are called primitive types.The following type is the primitive type.

Sbyte/byte/short/ushort/INT/uint/long/ulong/Char/float/Double/bool/decimal/Object/string

Which of the following is the value type?

Sbyte/byte/short/ushort/INT/uint/long/ulong/Char/float/Double/bool/decimal/Enumeration (Enum)/Structure (struct)

So what is the reference type?

Reference object/string/array/boxed Value Type

Now, what are the differences between these two things? Please refer to the following example.

Public class usefulvalues
{
Public static readonly int startvalue = 105;
Public const int endvalue = 120;
}

if we look at the vs compiler alone, we cannot see the differences between startvalue and endvalue. However, you will understand a lot through the Anti-compiler observation.

the Code In the anti-compiler is as follows:

  Public   class   usefulvalues { ///   fields   Public   const   int  endvalue =  120  ;   Public   static   readonly   int   startvalue;  ///   methods   static   usefulvalues ();   Public   usefulvalues () ;} 

The static constructor is as follows:

Static Usefulvalues() {Startvalue =0x69;}

0x indicates hexadecimal (for example, our webpage color # ff8899, is in hexadecimal format). Calculate 16x6 + 9 = 105.

the compiler has specified the value for endvalue during compilation, the value of startvalue is assigned to a static constructor, which is called only at runtime.

Let's look at the Il code.

 
. FieldPublic StaticLiteral int32 endvalue = int32 (120). FieldPublic StaticInitonly int32 startvalue

Initonly Variable value assignment can only be a declared part or a static constructor in the same class

literal: nominal value. here, the endvalue constant is replaced with its literal value of 120.

The most important difference between the runtime constant and the compilation constant is that the runtime Constant Value Analysis occurs at runtime, while the compilation Constant Value Analysis occurs at compilation. In other words, the Il code after compilation is often used to reference the address of the readonly variable rather than its value; when compiling, the compiled il code will directly reference its value.

Here is a summary of Microsoft's official explanation:  Readonly   Fields can be initialized in declarations or constructors, readonly field can be initialized either at the Declaration or in a constructor. "data-guid =" 257637d5ffda9953f70ee09ecc321_c "> Therefore, according to the constructors used,Readonly Fields may have different values. . readonly keyword is different from the const keyword. "data-guid =" b4a82ef93414a8b268308ea19c34187e "> const field can only be initialized at the declaration of the field. "data-guid =" 6a30dad9525e3e4f6691474252640dd7 "> const Segment can only be initialized in the declaration of this field .

Of course,If it is a static read-only field, this value can only be modified in the initialization phase or static constructor.. Therefore, I think this is why the database connection string will be defined as a static read-only field.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.