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 - C# Tutorial
From the course: C# Essential Training 1: Types and Control Flow
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…
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
-
-
-
-
Understanding classes, structs, and records7m
-
(Locked)
Defining constructors7m 6s
-
(Locked)
Object initialization6m 4s
-
(Locked)
Initialize only properties4m 59s
-
(Locked)
Cloning and copying objects9m 32s
-
(Locked)
Equality comparisons5m 27s
-
(Locked)
Defining abstract classes7m 52s
-
(Locked)
Creating static classes5m 18s
-
(Locked)
Solution: Expressing equality1m 5s
-
-
-
-
-
-