Join Reynald Adolphe for an in-depth discussion in this video Constants vs. read-only fields, part of C# Best Practices for Developers.
- [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 read-only one and it will be static. Specifying readonly and it'll be a type of integer. And we'll give it the name ReadOnlyVariable. And give it the value of zero.
Now I can write out the values of both of them. And at this point, if I try to assign a value to ConstantVariable the compiler will complain.
And lets pay careful attention to what it says. The left-hand side of an assignment must be a variable, property, or indexer. So this is not going to work. We can only assign a constant value up here on line 7. Now, if we try to do the same for read-only, the compiler also complains but you'll notice that the error is different. It says, a static readonly field cannot be assigned except in a constructor. And that is where one of the main differences lies.
If I created a constructor and get rid of the word public, because that will cause us an issue, I can assign a value of let's say five to readonly. And based on this error here, we know that we need to specify the program as static. So now if I run, I'll get the value of a 100 and five.
100 for the constant and five for the readonly. Readonly did not indicate a zero. There was no issue. So, I like to think of it as if I ever need a constant that is going to be one value throughout the lifecycle of a program I'll use constant. If I know that I need to have it initialize at some point later on, such as in a constructor, I can use readonly. Now, one thing that I'd like to point out before we move on to best practices is a change that we need to make here.
You can pause the video and look at the code and wonder what it might be. Now, if you decided that it has to do with like the naming of the variable, then congratulations, you're correct. We're so used to thinking of variables as placeholders for values we sometimes tend to forget that the actual definition is that the number can change. So that doesn't apply when it's a constant. So it's kind of a contradiction to have a variable named ConstantVariable. So let's go ahead and use the renaming feature in C# to rename that.
I'll change this to something a little bit more appropriate like ConstantNumber. And we'll do the same for ReadOnlyVariable, and change that to ReadOnlyNumber. We'll save. Execute. Make sure nothing broke. And everything works just as it did before.
So when we think in terms of best practices using constants and read-only, definitely use meaningful names and have them be using PascalCasing. And understand that constants are for compile-time. Whereas read-only are for runtime. In both cases, do not use uppercase or abbreviations. And only use them when you know in general that the fields are not going to have their values change.
- Naming and handling classes
- Running a unit test
- Using constructors
- Naming and handling methods, fields, and properties
- Using properties correctly
- Managing objects efficiently
- Common design patterns as best practices