From the course: Advanced C#: Object-Oriented Programming

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Read-only members

Read-only members

- [Instructor] In some cases, you might want to prevent changes from being made to certain member fields in your classes, and you can accomplish this by using the readonly keyword. So here in our ReadonlyProps folder in chapter two, let's open up our program code and let's open up our readonly example. For this example, I have a class definition that represents a book. It has an ISBN number, a title, and an author. And I also have an update method that lets me change the values of these private class members here at the top of the code. My main program creates a few instances of the class and then calls the update method to change the values. So let's just run the code as it currently is. And you can see that the values for the book1 are changed using the update method. What I'd like to do is prevent the ISBN number from being changed. So once you create the book record, I don't want ISBN number to be changed. I want it…

Contents