What is the difference between constant and readonly in C#?

Technology CommunityCategory: C#What is the difference between constant and readonly in C#?
VietMX Staff asked 3 years ago

Constant (const) and Readonly (readonly) both looks like same as per the uses but they have some differences:

  • Constant is known as “const” keyword in C# which is also known immutable values which are known at compile time and do not change their values at run time like in any function or constructor for the life of application till the application is running.
  • Readonly is known as “readonly” keyword in C# which is also known immutable values and are known at compile and run time and do not change their values at run time like in any function for the life of application till the application is running. You can assay their value by constructor when we call constructor with “new” keyword.