From the course: Advanced C#: Object-Oriented Programming

Using object initializers

- [Instructor] One of the convenience features that C# provides is the ability to create and initialize objects without having to write a separate constructor statement followed by a lot of individual assignment statements. So in our code here in the Object Initializers folder in chapter two, let's open up our program file and let's open up Initializers.cs. So consider the code I have here in my Initializers file that defines a class hierarchy of pets with a pet-based class and then I've got two derived classes, Dog and Cat. We have public properties for things like name and age and then IsTrained for dogs and IsDeclawed for cats. And then in the main program, let's exercise some of this code. I can create and initialize instances of these classes at the same time. So what I'm going to do is make a new Dog object and it's going to be a new dog. And what I'm going to do is inside curly braces just initialize each of those public properties directly by using their name and a value. So for example, I'll have the dog's name be Fido and the age is going to be four and IsTrained will be true. And then we'll make a cat and the cat's name will be Mr. Meowington and the age is going to be seven and IsDeclawed will be false. All right, and then I can print out each one just to make sure that it worked. So that's going to be the Dog object and we'll have dog.Name and dog.Age and we'll copy and paste that and do the same thing for the cat. Right. Now obviously this only works for the properties and fields that are accessible to the code when the object is constructed. And since my class has public properties, I can access all of them. But remember what we learned about earlier when we covered access modifiers because those control what you can and can't initialize. So if we go back to the Initializers, if this was, you know, private, for example, then I wouldn't be able to see the IsTrained property and I would not be able to initialize it. All right, so let's go ahead and run what we have so far. And oh, whoops. I have to put the dollar sign outside the- There we go. Okay, let's try that again. There we go. Okay, so as expected, we have two objects, one dog and one cat with the values properly initialized. And this also works on anonymous types which we learned about in the last chapter. So for example, I can write something like var pet equals new. And then, you know, name equals Charlie. And, you know, age is five, right? And then let's just do the same thing with our pet. So I'll copy and paste this. All right. And let's run that one more time and we can see that that works as well, okay? You can also initialize collections this way. So let's use an array of integers as an example. And what I'm going to do is comment out these previous examples so that we don't clog up the output. So I'm going to create an array of integers called numbers and then to initialize that right alongside the declaration, I can just simply write new int, and then inside the curly braces, use the values that I want to have be in the array. And then I'll just Console.WriteLine out and I'll just delete that and put in numbers.Length. All right, so let's go ahead and try that. And yes, we can see that there are six numbers in the array that I just defined. So let's take another look at the pet classes. Let's go ahead and close this terminal for now. So let's scroll down to the bottom. And you can see that there's a class here named PetOwner and it has a public name property, which is a string. It also has a list collection that lists the pets owned by a particular person. So let's write some code that initializes this class for a pet owner that has a few pets. Let's go back to the program and I'm going to create a new instance of the PetOwner class. So I'll create a new pet owner, and then inside the curly braces, I'll just set the name property and once again that'll be me. And then I'm going to initialize the collection, the Pets collection. That's going to be a new list of Pet, and inside that list I'm going to put a new Dog. And the name is going to be Junebug and the age is going to be four. And the new Cat name will be Whiskers and the age will be three. And then finally another new Dog whose name will be Max and age will be 10. And then let's write out the contents to make sure that it worked. So we'll write out the owner's name so owner.Name and apostrophe S Pets. And then I'm going to have a foreach loop. And I'm going to loop over each pet in owner.Pets. And we'll just simply print out, put out the name of each pet, and let's go ahead and comment out the integer example. All right, so let's run our updated code. Let's get that terminal back here again. And, all right, and now we can see here's, sure enough, my list of pets. We've got Junebug, Whiskers, and Max. So object initializers lets you write more concise code that combines instantiating an object with the readability of initializing the object's values.

Contents