From the course: Advanced C#: Object-Oriented Programming
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Required properties - C# Tutorial
From the course: Advanced C#: Object-Oriented Programming
Required properties
- [Instructor] If you define classes that provide public properties, there are probably going to be times when you want consumers of your class to be required to initialize some of them. So starting in C Sharp 11, this became possible by using the required modifier and we're going to see how it works in this example. Here in our required props folder, let's open up the program code as well as requiredexample.cs. So in my code here, I have a class that represents some employee information. I have a default constructor and some public properties which allows the class to be instantiated using object initializers, which we learned about earlier. And in fact, if you look at the program code, you'll see that there are a few instances of employees being created using the object initializer syntax. Suppose however, we wanted to make sure that certain properties were set whenever an employee object was constructed in this way. To do…