Difference between readonly and const (C #)

Source: Internet
Author: User
1. Both readonly and const are used to identify constants. 2. const can be used to modify the field of the class or a local variable (localvariable); readonly is only used to modify the field of the class. 3. The value of the const constant must be clear and constant during compilation, but the readonly constant is a little different.

1. Both readonly and const are used to identify constants. 2. const can be used to modify the field of the class or a local variable (local variable); readonly is only used to modify the field of the class. 3. The value of the const constant must be clear and constant during compilation, but the readonly constant is a little different.

1. readonlyAndConstAre used to identify constants.

2.ConstIt can be used to modify the field of a class or a local variable, while readonly is only used to modify the field of a class.

3.ConstThe value of a constant must be clear and constant during compilation, but the value of a readonly constant is a little different, that is, the value can be compiled at runtime. Of course, it must also comply with the constraint as a constant, that is, the value must be constant.

4.ConstA constant must be assigned a value while being declared, and the value must be fixed and constant during compilation; the readonly constant can choose to assign a fixed value to it during compilation at the same time as needed, or assign the initialization work of its value to the instance constructor) complete. For example:Public readonly stringM_Now = DateTime. Now. ToString ();, m_Now will change with the actual situation during running.

5.ConstConstants belong to the class level rather than the instant object level, and cannot be used together with static, the value of this constant will be shared by all instance objects of the entire class (For details, refer to the Remark area later ).

6. readonly constants can be class-level or Instance Object-level, depending on its declaration and implementation of initialization. Readonly can be used in combination with static to specify that the constant belongs to the class level and submit the initialization work to the static constructor) complete (for details about how to declare a readonly constant as a class or instance object level, see the Remark area later ).

7. can beConstThe type declared as a constant must be the following primitive type (primitive type): sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, float, bool, decimal, string.

8. object, Array (Array)AndThe structure (struct) cannot be declaredConstConstant.

9. Generally, the reference type cannot be declaredConstConstant, but there is an exception: string. This reference typeConstThe constant value can be string or null. In fact, although the string type is a reference type,. NET has special processing on it. This kind of processing is called the immutable character string, so that the string value has the read-only feature.

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.