Simplifying methods with operators
We might want two instances of a person to be able to procreate.
Implementing some functionality with a method
Add the following method to the Person
class:
// method to "multiply" public Person Procreate(Person partner) { var baby = new Person { Name = $"Baby of {this.Name} and {partner.Name}" }; this.Children.Add(baby); partner.Children.Add(baby); return baby; }
At the top of the Program.cs
file, type the following code to import the namespace for our class and statically import the Console
type:
using Packt.CS7; using static System.Console;
Now, we can get two people to make a baby by adding the following to the Main
method of the Program.cs
file:
var harry = new Person { Name = "Harry" }; var mary = new Person { Name = "Mary" }; var baby1 = harry.Procreate(mary); WriteLine(...