[C # Basics] Analysis of Common const and readonly pen questions

Source: Internet
Author: User

I haven't updated my blog for a few days. I found that I had to write my blog to the point where I was forced to write my blog. Although I was very tired, I couldn't forgive myself for not writing anything. Today, why do we sum up the differences between the two keywords? I always feel that these two keywords are used too often. I have never thought about why they are used. It is like why I have always used chopsticks with my right hand. Why don't I use chopsticks with my right hand? Suddenly you did this, and found that you are not in coordination with the surrounding area, and you still cannot get food. The same is true for const and readonly. I am used to it. I have never pursued it if I keep using it like this. I was suddenly asked, but I really can't say a second or a second. Today I will study it in detail. What is this stuff? Although I have read many of this content on the Internet, it is summed up by others. If I do not practice it myself, I feel that it is not my own. Practice can be remembered more deeply and understood more deeply. Constant static constant: a constant is parsed by the compiler during compilation, and the constant value is replaced with the initialized value. Dynamic Constants: Get the value at the moment of running. during compilation, the value is identified as a read-only constant instead of a constant value. In this way, Dynamic Constants do not have to be initialized during declaration, the initialization can be delayed in the constructor. The constants modified by readonly and constconst are static constants, while those modified by readonly are dynamic constants. How is the difference? Const-modified constants must be initialized during declaration, while readonly-modified constants can be delayed in constructor initialization. The const-modified constant is parsed during compilation, that is, the constant value is replaced with the initial value, and the readonly-modified constant is delayed until it is run. The const modifier focuses on efficiency, while the readonly modifier focuses on flexibility. The const modified constants do not consume memory. readonly consumes memory because it needs to save constants. Const can only modify primitive types. This restriction is not available for the enumerated class or string type readonly. Question 1: copy the Code 1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace Wolfy. constReadonly 8 {9 class Program10 {11 static readonly int A = 2 * B; 12 static readonly int B = 4; 13 static void Main (string [] args) 14 {15 Console. writeLine ("A = {0}, B = {1}", A, B); 16 Console. read (); 17} 18} 19} copy the code so A = ?, B = ?, Let's take a look at what it is: Why? ILspy, let's take a look. What is going on? There is no difference in this way. Don't worry. Let's take a look at the comparison and you will know the difference. To illustrate the static fields: copy the Code 1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace Wolfy. constReadonly 8 {9 class Person10 {11 public static readonly int C; 12 static Person () 13 {14 C = 3; 15} 16} 17} copy the code to write the static Fields described in red in the preceding figure. Let's change readonly to const for a try. Question 2: This is mainly to check whether static can modify a variable at the same time with the const keyword, and report an error during compilation. Question 3: copy the Code 1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace Wolfy. constReadonly 8 {9 class Program10 {11 const int A = 2 * B; 12 const int B = 4; 13 static void Main (string [] args) 14 {15 Console. writeLine ("A = {0}, B = {1}", A, B); 16 Console. read (); 17} 18} 19} copy the code so A = ?, B = ?, You know, don't worry too much. Let's take a look at what it is: what is the IL of this interview question? To facilitate the comparison, paste the IL part of readonly to facilitate the comparison. The const-modified constant is parsed during compilation, that is, the constant value is replaced with the initial value, and the readonly-modified constant is delayed until it is run. Through the comparison of IL, I have a deeper understanding of their differences. Hope to help you. Question 4: The main test is that the const modified constant must be initialized. The summary is very basic. I just want to study it. I checked it through IL. Although the basics, if I found out through IL, I still learned a lot, if it is helpful to you, we recommend it. Thank you!

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.