Const and readonly constants, constreadonly Constants

Source: Internet
Author: User

Const and readonly constants, constreadonly Constants

Const and readonly Constants

Const and readonly are used to define constants, but what are their differences?

The following is a brief description:

The constant modified by const is the compile-time constant, for example: public const String PI = 3.1415; what is the compile-time constant is that when you declare, A value must be assigned (that is, initialization). If no value is assigned, an exception occurs during compilation that says "a constant field requires a value.

Readonly modifies the runtime constant. You can assign values in the Declaration or in the constructor (note that values can only be assigned in these two places ).

Http://www.cnblogs.com/royenhome/archive/2010/05/22/1741592.html (here reprint this article), the article is very thorough, but still want to use their own words, to more popular description again.

C # There are two different constant types in the language, namely static constants and Dynamic Constants. Static constants mean that the compiler first parses constants, and replace the constant value with the initialized value (that is, the compiler will spread the constant ). For example, the following code:

class Program {public static void Main(string[] args) {Console.WriteLine("A is {0},B is {1} ", P.A,P. B);Console.ReadKey();}}class P {public static const int A = B * 10;public static const int B = 10;}

The console outputs A is 100, B is 10, that is, the constant is parsed and the initial value is replaced.

 

A dynamic constant is obtained at the moment when the constant value is running. The program does not parse the constant during compilation, so it is the default value when it starts execution, if it is int type, the default value is 0. the following code:

class Program {public static void Main(string[] args) {Console.WriteLine("A is {0},B is {1} ", P.A,P. B);Console.ReadKey();}}class P {public static readonly int A = B * 10;public static readonly int B = 10;}

The console outputs A is 0 and B is 10.

 

What is the same role of const and readonly?

That is to ensure that the constant fields or constant local variables modified by the two are not modified due to coding errors in subsequent projects. Of course, for some frequently modified values, do not use const or readonly for modification.

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.