From the course: C# Essential Training 1: Types and Control Flow

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Initialize only properties

Initialize only properties

- [Instructor] We've talked about different ways to initialize your objects. We want to go back to our properties here. As we get into a pattern much like you see here, where I have just an auto implemented property with a get and a set. There are different options for what you do there. If we look at our PremiereCustomer, you can see I have a CustomerLevel and I have a get, but I also have an init, meaning this can only be set with the value changed when initialized, what that looks like is if I come down here, we try and create one of these. So I'll do PremiereCustomer = new PremiereCustomer(), and I've got only an empty constructor I can do FirstName = "New Customer". And now if I try and do pcust CustomerLevel = two, say that's going to give me a value. I'm immediately getting compiler warnings that say that's an init only property. You can only set that during initialization. So what does that mean? Well, I could just come in here and do a constructor that takes a byte and that…

Contents