From the course: C# Best Practices for Developers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Constants vs. read-only fields

Constants vs. read-only fields - C# Tutorial

From the course: C# Best Practices for Developers

Start my 1-month free trial

Constants vs. read-only fields

- [Instructor] I'd like to talk about the difference between constant and read-only variables. First thing to point out is that constants are compile-time whereas read-only variables are runtime constants. Next is that a constant can be assigned on declaration and that's it. Whereas with read-only it can be assigned on declaration or constructor. A constant can only be a number, boolean or string. Whereas with read-only variable it can be any data type. A constant is always static. However, read-only variable is optionally static. Now, let's go through some examples here. Here in Visual Studio I'm in the program file within the Prestige.Biz project. And I'll start off by cleaning up my using statements and get rid of what I don't need. I'll keep System because I know I'm going to be using that. But, let's go ahead and now create a constant and we'll use the const keyword to do so. It'll be an integer and we'll name it ConstantVariable. Assign it a value of 100. Now, I will do a…

Contents