Inheriting and extending .NET types
.NET has pre-built class libraries containing hundreds of thousands of types. Rather than creating your own completely new types, you 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:
- In the
PacktLibrary
project, add a new class file namedPersonException.cs
. - Modify the contents of the file to define a class named
PersonException
with three constructors, as shown in the following code:namespace Packt.Shared; public class PersonException : Exception { public PersonException() : base() { } public PersonException(string message) : base(message) { } public PersonException(string message, Exception innerException) : base(message, innerException) { } }
Unlike ordinary methods, constructors are not inherited...