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 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 namedPersonException
, with three constructors, as shown in the following code:using System; 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, so we must explicitly declare and explicitly...