ASP. NET learning notes (11) Difference between const and readonly

Source: Internet
Author: User

Readonly indicates read-only, and const indicates constant
I. They are used to identify constants.

2. const must be assigned a value while being declared;
Readonly can be assigned a value during initialization. Therefore, the readonly field may have different values based on the constructors used.

3. the const field is the compile time, And the readonly field can be used to run the time.
1. const requires that a definite value be calculated during compilation to replace every place that calls this constant. Therefore, the value cannot be extracted from the variable to initialize the constant.
2. readonly is executed during computation and is determined only at runtime.

4. By default, const is static. If readonly is set to static, it must be explicitly declared.

5. The reference type modified by const can only be string or other reference types whose values are null.

6. Objects, arrays, and struct cannot be declared as const constants.

7. readonly can only be used to modify the field of a class. It cannot modify local variables or other class members such as property.
By: dxh_0829

Related Article

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.