Inheriting and extending .NET types

.NET has prebuilt class libraries containing hundreds of thousands of types. Rather than creating your own completely new types, you can often start by inheriting from one of Microsoft's.

Inheriting from an exception

In the Ch07_PacktLibrary project, add a new class named PersonException, as shown in the following code:

    using System; 
 
    namespace Packt.CS7 
    { 
      public class PersonException : Exception 
      { 
        public PersonException() : base() { } 
        public PersonException(string message) : base(message) { } 
        public PersonException(string message,  
          Exception innerException) : base( 
          message, innerException) { } 
      } 
    } 

In the Person class, add the following method:

 public void TimeTravel(DateTime when) { if (when <= DateOfBirth) { throw ...

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.