1. The similar feature Similarity
GRAMMAR:
Public const int x = 100;
Public readonly int X;
Const variable and readonly variable can not be modified once they are initialized in run-time. However, the main differences between them is the time to be initialized!
The value of the const and readonly variables cannot be changed once initialized. However, the difference between them is that they are initialized at different times.
2 the differences
const variable must be initialized when it's be declared, because its value is set in compile-time before the respective object is constructed. however, readonly can be initialized dynamically. that is to say, its value can be set via class constructor or vairable initiazer (no other feasible ways ).
the const constant must be declared Initialization, because its value is set before the corresponding object structure is compiled. However, readonly can be set dynamically. It can be set either during initialization or in the constructor (other methods are not supported ).
E. G
Public Class A
{< br> Public const m_x = 100;
// Public const m_x = datatime. Now. tricks;
// error! Datatime. now. tricks can not give a exact value for const variable in compile-time.
Public readonly long m_y = datatime. now. tricks;
// It can be initialized via viriable initiazer
Public readonly int m_z;
Public A ()
{< br> m_z = datatime. now. tricks;
// It also can be initialized via constructor;
}< BR >}< br>