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 - C# Tutorial
From the course: C# Best Practices for Developers
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Method overloading5m 16s
-
(Locked)
Unit testing without parameters4m 5s
-
(Locked)
Unit testing with parameters2m 54s
-
(Locked)
Method chaining4m 7s
-
(Locked)
Constants vs. read-only fields5m 28s
-
(Locked)
Using properties correctly2m 11s
-
(Locked)
Using properties demo4m 29s
-
(Locked)
Auto-implemented properties1m 33s
-
(Locked)
Auto-implemented properties demo5m 13s
-
(Locked)
-
-
-