Inheriting and Extending .NET Types

Learn about inheriting exceptions in .NET to create custom exception types and extend existing types using static and extension methods for added functionality.

.NET has pre-built class libraries containing hundreds of thousands of types. Rather than creating our own completely new types, we can often get a head start by deriving from one of Microsoft’s types to inherit some or all of its behavior and then overriding or extending it.

Inheriting exceptions

As an example of inheritance, we will derive a new type of exception:

Step 1: In the PacktLibrary project, add a new class file named PersonException.cs.

Step 2: Modify the contents of the file to define a class named PersonException with three constructors, as shown in the following code:

Get hands-on with 1400+ tech skills courses.