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.
Object initialization - C# Tutorial
From the course: C# Essential Training 1: Types and Control Flow
Object initialization
- [Instructor] Well, you may quickly find, if you're not already familiar with it from your programming experience, is that defining constructors that meet everyone's need can be kind of challenging. You add the parameters you think people will use, but it's not exactly what they want. Rather than creating 10 or 15 different constructor options we can use these default parameters and really if we're going to do this correctly we should probably make this the employee ID property on our employee instead of the ID and we'll just fix that while we're here. But we've got all these other properties, so we've got age, we've got the employee ID, start date, shift start time, all these different pieces. So one of the things we can use in C# is called the object initialization. So rather than do the employee this way, I can come in and I can use curly braces rather than parentheses and now I can put the property name in. So I can say first name equals Matt, last name equals Milner. And I can…
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
-
-
-
-
-
-