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.

Init-only properties

Init-only properties

- [Instructor] In a previous video we saw how to use the read-only keyword to prevent changes to member variables of a class after they had been initialized. In some cases, you want to be able to do the same thing with properties. That is create properties that can only be initialized with a value and then become read-only afterward. This can be accomplished in C sharp using what are called nit only setters, which is a feature that was introduced back in version 9 of C sharp. By using this feature, you can enforce immutability so that once the property value is set, it can't be changed again. So here in our code for init props in chapter two let's open up our program code and let's open up our init example file. Here in my example code I have a class that represents some employee data along with some program code that creates and exercises the employee class. So each employee has an ID, a first and last name, and…

Contents