Difference between const and readonly

Source: Internet
Author: User

  1. Both readonly and const are used to identify constants.
  2. Different initialization values
    The constant modified by const must be assigned a value at the same time as the declaration.
    The readonly field can be assigned a value during initialization (declaration or constructor. You cannot assign values elsewhere. Otherwise, the compiler reports an error. Therefore, the readonly field may have different values based on the constructors used.
    Readonly is an instance Member, so different instances can have different constant values, which makes readonly more flexible.
    1. The const field is the compile-time amount, while the readonly field can be used for the run-time amount.
      The constant declared by constf, during compilation, replaces each place that calls the constant with the calculated value. For example:
      Public const int n = 1;
      Public const int m = n + 1; // correct
      Public const int;
      Public const int B = a + 1; // error. a is a variable and cannot be calculated during compilation.
      Note: The n declared by const is called in the program. In the compiled intermediate language code, it is replaced by 1. If you want n = 2, you need to recompile the current class library; otherwise, the place where n is called is still 1.

      Readonly allows a field to be set as a constant, but some operations can be performed to determine its initialization value. Because readonly is executed during computation, you can use some variables for initialization. This value is determined only at runtime. For example:
    1. 01
    class Person 02 { 03     public readonly uint nowtime = (uint)DateTime.Now.Ticks; 04 } 05 class Program 06 { 07     static void Main(string[] args) 08     { 09         Person p = new Person(); 10         Console.WriteLine(p.nowtime); 11         Console.ReadKey(); 12     } 13 }
  1. The const is static by default, and the readonly statement must be displayed if it is set to static.
  2. The value types modified by const are limited. Objects, arrays, and struct cannot be declared as const constants. Readonly can be of any type.
    That is to say, when we need a const constant, if its type limits it, it cannot be calculated for a definite value during compilation, we can resolve this problem by declaring it as static readonly. However, there is a slight difference between the two: Since the const field is the number of compilation times, the readonly field can be used for running times. Therefore, if you reference a constant declared by const in another file and modify the value of the constant, You need to recompile all the files associated with the constant; if this constant is declared using static readonly, you only need to compile the file that initializes the constant.
    Check whether the following const and static readonly can be exchanged:
    1) static readonly Class1 c1 = new Class1 (); // it cannot be changed to const. The new operator is required to execute the constructor, so it cannot be determined during compilation. Const must be assigned a value while being declared.
    2) static readonly Class1 c1 = null; // you can change it to const. The reference type that can be declared as const can only be string or null.
    3) static readonly a = B * 20;
    Static readonly B = 10; // you can change it to const.
    4) static readonly int [] array = new int [] {1, 2, 3}; // it cannot be changed to const. The principle is the same as 1.
    5) void SomeFunction ()
    {
    Const int a = 10; // The value cannot be changed to readonly.Readonly can only be used to modify the field of a class. It cannot modify local variables or other class members such as property.
    }

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.