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 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.