Const VS readonly

Source: Internet
Author: User

Some time ago, I saw a post on CSDN asking about the difference between const and readonly. I found it interesting. So I found some materials and wrote some code to study it.

The differences between constants and read-only variables are as follows:
1. A constant must be initialized during declaration and cannot be modified after a value is specified. the read-only field can be initialized during declaration, or the initialization value can be specified in the constructor. The value cannot be modified after construction.
2. constants are static, while self-read fields can be static and dynamic.
3. Const can be used in fields and local variables. readonly can only modify fields.

Below is my test code

Using System;

// Description: fields without numbers are not initialized during Declaration and are initialized during construction.
// Fields numbered 1 are initialized during declaration and not during construction
// Fields numbered 2 are initialized during the Declaration and Construction
// Fields numbered 3 are not initialized during the declaration.
Public class Test
...{
// Constant
// The constant must be initialized at the time of declaration. The following statement indicates a compilation error. "The constant field requires a value"
// Const string myConst;
Const string myConst = "initialized upon declaration ";
// Static read-only
Public static readonly string myStaticReadonly;
Public static readonly string myStaticReadonly1 = "initialized upon declaration ";
Public static readonly string myStaticReadonly2 = "initialized upon declaration ";
Public static readonly string myStaticReadonly3;

// Read-only
Public readonly string myReadonly;
Public readonly string myReadonly1 = "initialized during Declaration ";
Public readonly string myReadonly2 = "initialized during Declaration ";
Public readonly string myReadonly3;

// Default constructor
Public Test ()
...{
MyReadonly = "the default constructor is initialized ";
MyReadonly2 = "the default constructor is initialized ";
}

// Constructor with Parameters
Public Test (string arr)
...{
MyReadonly = arr;
MyReadonly2 = arr;
}

// Static constructor, which does not contain parameters
Static Test ()
...{
MyStaticReadonly = "initialized during Construction ";
MyStaticReadonly2 = "initialized during Construction ";
// The constant value cannot be changed in the constructor. A compilation error occurs in the following statement: "variable, attribute, or indexer must be on the left of the value assignment number"
// MyConst = "value ";
}

// The test uses static methods to change the values of constants and self-read variables.
Public static void StaticChangeValue ()
...{
// The constant value cannot be changed. The following statement may cause a compilation error. "The value must be a variable, attribute, or indexer on the left"
// MyConst = "changed ";

// The value of the Self-read field cannot be changed after the construction. The following statement shows a compilation error. "The value of the static read-only field cannot be assigned (except for the static constructor or the variable's Initial Value Setting )"
// MyStaticReadonly = "changed ";
}

// The method used in the experiment to change the value of the Self-read variable
Public void ChangeValue ()
...{
// The value of the Self-read field cannot be changed after the construction. The following statement shows a compilation error. "The value of the read-only field cannot be assigned (except for the specified item of the constructor or variable Initial Value )"
// MyReadonly = "changed ";
}

// Test static local constants and self-read Variables
Public static void Include ()
...{
Const string includeConst = "local static constant ";
// Readonly cannot modify static local variables
// Static readonly extends dereadonly = "local static self-read variable ";
}
// Test Local Self-read Variables
Public void StaticInclude ()
...{
// Readonly cannot modify local variables
// Readonly staticIncludeReadonly = "local static self-read variable ";
}
Public static void Main ()
...{
// See static data
Console. WriteLine ("Test. myConst:" + Test. myConst );
Console. WriteLine ("Test. myStaticReadonly:" + Test. myStaticReadonly );
Console. WriteLine ("Test. myStaticReadonly1:" + Test. myStaticReadonly1 );
Console. WriteLine ("Test. myStaticReadonly2:" + Test. myStaticReadonly2 );
Console. WriteLine ("Test. myStaticReadonly3:" + Test. myStaticReadonly3 );
Console. WriteLine ("");

Test test = new Test ();
Console. WriteLine ("test. myReadonly:" + test. myReadonly );
Console. WriteLine ("test. myReadonly1:" + test. myReadonly1 );
Console. WriteLine ("test. myReadonly2" + test. myReadonly2 );
Console. WriteLine ("test. myReadonly3" + test. myReadonly3 );
Console. WriteLine ("");

Test test1 = new Test ("initialized when constructing a function with Parameters ");
Console. WriteLine ("test1.myReadonly:" + test1.myReadonly );
Console. WriteLine ("test1.myReadonly1:" + test1.myReadonly1 );
Console. WriteLine ("test1.myReadonly2" + test1.myReadonly2 );
Console. WriteLine ("test1.myReadonly3" + test1.myReadonly3 );
Console. WriteLine ("");

Console. WriteLine ("from the result, we can see that the initialization value during the construction will overwrite the initialization value during the Declaration ");
Console. WriteLine ("the read-only field not initialized is assigned the default value, null value ");

Console. Read ();
}
}

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.