C #: const and readonly usage and Difference Analysis

Source: Internet
Author: User

C #: const and readonly
To sum up const and readonly:

The const and readonly values can no longer be rewritten once initialized;
Const can only be initialized at Declaration; readonly can be initialized at Declaration or in constructor;
Const implies static, and static const cannot be written again; readonly is not static by default, and static readonly can be written as needed;
Const is a constant for static parsing during the compilation period (so its expression must be evaluated during compilation); readonly is a constant for dynamic parsing during the runtime;
Const can be used to modify the members of a class or the local variables in the function body. readonly can only be used to modify the members of a class.
I extracted the text from the Internet, too many words, so I am too lazy to summarize and type on my own.

Note: The fourth point is especially important. I use the following code to describe and verify it:

1. Create a text file and change it to The. CS file-Lib. CS:

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace constreadonly_base <br/> {<br/> public class test <br/>{< br/> Public const double Pi = 3.14; <br/> Public static readonly double Pi = 3.14; <br/>}< br/> using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace constreadonly_base <br/> {<br/> public class test <br/>{< br/> Public const double Pi = 3.14; <br/> Public static readonly double Pi = 3.14; <br/>}</P> <p>

2. Enter CSC/T: Library Lib. CS in the command line to generate a Lib. dll file.

3. Create a text file and change it to The. CS file-demo. CS:

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace constreadonly_demo <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/> {<br/> console. writeline (constreadonly_base.test.pi); <br/> console. writeline (constreadonly_base.test.pi); <br/> console. readkey (); <br/>}< br/> using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace constreadonly_demo <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/> {<br/> console. writeline (constreadonly_base.test.pi); <br/> console. writeline (constreadonly_base.test.pi); <br/> console. readkey (); <br/>}</P> <p>

4. Enter CSC/R: Lib. dll demo.csin the command line to generate a demo.exe file.

Double-click. The result is as follows:

3.14

3.14

5. Change Lib. CS:

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace constreadonly_base <br/> {<br/> public class test <br/>{< br/> Public const double Pi = 3.1415; <br/> Public static readonly double Pi = 3.1415; <br/>}< br/> using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace constreadonly_base <br/> {<br/> public class test <br/>{< br/> Public const double Pi = 3.1415; <br/> Public static readonly double Pi = 3.1415; <br/>}< br/>

6.double-click the demo.exe file, but the result is as follows:

3.14

3.1415

(Note: directly double-click demo.exe is run directly without compilation. The above result is the result of the end of the runtime, which is equivalent to skipping compilation)

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/xiaofengsheng/archive/2009/10/12/4659892.aspx

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.