Join Reynald Adolphe for an in-depth discussion in this video Initializing objects, part of C# Best Practices for Developers.
- [Instructor] Throughout the course we've touched on the topic of instatiating objects a few times. But I'd like to revisit it so that we can think of managing our objects in an efficient manner because when we're initialing objects it can be done in one of three ways: Parameterized constructors, Object initializers, and setting properties of an object. Now, we've used a couple of these already in our code, and let's go ahead and review them. In Visual Studio, if we jump to our actor test, and scroll down, for me it will be on line 83.
Here's an example of where we are using an object initializer. We're instantiating the actor object and within curly braces, we're assigning the value of Sandy Love to the property, actor name. Now, this comes in very useful when readability is concerned. Over here we only have one property that we're assigning values to, but if we go to this website here, and this has a little bit more detail, but you can tell by the first example, if we had five properties, and 10 lines of code, it's a lot easier to read and get a good glance of what the property values are, versus if it wasn't laid out in this manner.
Now, if we go to line 25. Here's an example where we are using a parameterized constructor. We instantiate the object, and within parenthesis, we pass in, Johnny Boy, and on the backend that gets populated to the property. And this is really useful when we're just dealing with a basic set of properties. Now for our third example, where we are setting properties we can go ahead and create a test that demonstrates this and I'll just copy and paste what we have here.
And we'll call this TestSettingObjectProperty. And we're just going to make a change on two lines. The first is getting rid of Johnny Boy. That's being passed into the constructor. And right below, on the property actor name, we found the value.
And I can save this and test to make sure that it runs as expected and passes. And it does. A very straight forward change. So to recap in terms of best practices. Know that for setting properties it's best to set them when populating from database values for example, because using the properties makes it easier to perform conversions from database types.
If something goes wrong, well then a debugger points us to the exact property. Also, this approach makes it easier to modify individual properties any time after initialization. Parameterized constructors are used when we are creating a basic set of properties. And it also can be used to initialize an object to a valid state. Now in terms of object initialization, that's good for when initializing a subset or superset of properties.
But really in my opinion, its best feature, its very easy to get a good glance of what all the property values are, quickly.
- Naming and handling classes
- Running a unit test
- Using constructors
- Naming and handling methods, fields, and properties
- Using properties correctly
- Managing objects efficiently
- Common design patterns as best practices