I am working overtime on Saturday. I can go later. When I get up in the morning, I suddenly think of the similarities and differences between const and readonly in. net. I once asked a candidate for an interview at the company when I was recruiting, but he gave me an explanation of the chinese meanings of the two words. Now there are some developers who have asked him to do the project, but there may be a lack of things.
Let me talk about the knowledge points here.
Experience :
A knowledge point is used to get a piece of water at a time and practice it. Never forget. Next, let's talk about the differences that I understand.
1. the const field can only be in
Statement.
The readonly field can be found in
Declaration or constructor. Therefore, the readonly field may have different values based on the constructors used.
2. the const field is
Compile timeConstant, while the readonly field can be used
RuntimeConstant.
3. Const
Constant of the reference type. The possible values can only be string and null..
Readonly can be
Any Type Note: : When modifying the reference type of readonly, it should be understood that the reference address cannot be modified, and the internal field of the referenced object can be changed. I often use readonly in projects to implement "Single-value mode ". For example, the "memory table" in the system"
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.