Refactoring and inheritance
Now that we’ve covered some of the ways refactoring can help organize your code, let’s dive into refactorings related to inheritance. This is a collection of refactorings that involve either overriding methods, introducing inheritance, or altering in-place inheritance relationships to improve the maintainability of code.
Overriding ToString
ToString
is one of the four methods that any .NET object is guaranteed to have due to the virtual
definition of ToString
on System.Object
. This method is used whenever an object is converted to a string and can be particularly handy for logging and debugging purposes.
Sometimes overriding ToString
can simplify your code in unexpected ways.
Let’s look at the BuildFlightIdentifier
method in FreightFlightInfo.cs
. This method relies on the DepartureLocation
and ArrivalLocation
properties of type Airport
to produce a string:
FreightFlightInfo.cs
public string BuildFlightIdentifier() ...