Initializing objects

- [Instructor] Throughout the course we've touched onthe topic of instatiating objects a few times.But I'd like to revisit itso that we can think of managing our objectsin an efficient mannerbecause when we're initialing objectsit 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 exampleof where we are using an object initializer.We're instantiating the actor objectand within curly braces, we're assigning the valueof Sandy Love to the property, actor name.Now, this comes in very usefulwhen readability is concerned.Over here we only have one propertythat 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 easierto read and get a good glanceof 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 examplewhere 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 usefulwhen we're just dealing with a basic set of properties.Now for our third example, where we are setting propertieswe can go ahead and create a testthat demonstrates thisand 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 thisand testto make sure that it runs as expectedand passes.And it does.A very straight forward change.So to recap in terms of best practices.Know that for setting propertiesit's best to set them when populating from database valuesfor example, because using the propertiesmakes it easier to perform conversionsfrom database types.

If something goes wrong,well then a debugger points us to the exact property.Also, this approach makes it easier to modify individualproperties any time after initialization.Parameterized constructors are usedwhen we are creating a basic set of properties.And it also can be usedto initialize an object to a valid state.Now in terms of object initialization,that's good for when initializing a subsetor superset of properties.

But really in my opinion, its best feature,its very easy to get a good glanceof what all the property values are, quickly.

Resume Transcript Auto-Scroll

Author

Released

6/5/2018

Get into the habit of developing reliable, readable, and sustainable application code by following coding standards with C#. In this course, join instructor Reynald Adolphe as he shows how to go beyond the language syntax and master best practices that can help you produce high-quality C# code. Throughout the course, Reynald shares some of his favorite best practices, illustrating precisely when and where to use each one. Learn about right way to use constructors, manage objects, implement often ignored advantages of getters and setters. Plus, explore best practice resources, including those for essential design patterns.