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 ...

Get C# 7 and .NET Core: Modern Cross-Platform Development - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.