Lesson 04.4: Improving the World – Inheritance and INotifyPropertyChanged

In this lesson, we will do some refactoring – cleanup, and minor improvements to the code we wrote over the last few lessons.

This is a common practice. Many times, you need to try several ideas, to add in new features. Once you have them working, it’s a good practice to review the code, and see if there are any ways to improve it.

These changes will focus on improvements with the code to handle INotifyPropertyChanged.


Video version:


Improve the property name parameter, in the notification

Step 1: Open GameSession.cs.

Inside the “set” code, for CurrentLocation, we have these lines:


This uses string values to pass the name of the property that changed. This works, but might cause a problem later.

If we ever rename a property, the program will still compile and run. However, the UI won’t update – because the changed property has a new name.

We’ll change the code to use the nameof() function. This function takes the property as a parameter. If we ever rename the property, the parameter will update (if we use Visual Studio refactoring), or report an error when we compile the program (letting us know about the problem quickly).

Change these lines to:


Now, the parameter we pass to OnPropertyChanged will be updated, if we ever rename the property by using the “Rename” refactoring menu option.


Step 2: Open the Player class, and make the same changes where it uses OnPropertyChanged.

So, these lines:


Will now look like this:



Eliminate duplicated code, using inheritance

You might have noticed that we have duplicated code to handle INotifyPropertyChanged in the Player and GameSession classes.

You normally don’t want to have duplicated code. If you do, and you need to make a change, you need to remember all the places where that code exists – and make the change in every place.

It’s better to have the code in one place, and have all the classes use it from that one place. If you ever need to make a change, you make it in the one place, and it’s updated for every class that uses it.


One way to share code is to create a “base” class, and have your other classes “inherit” from that base class. That is what we’ll do in this lesson.


Step 1: In the Engine project, create a new class named “BaseNotificationClass.cs”

Make the class public, and have it implement INotifyPropertyChanged. Add the PropertyChanged event and the OnPropertyChanged() function. The code should look like this:


Step 2: Edit the Player and GameSession classes.

Currently, we have “INotifyPropertyChanged” after the lines where we start the Player and GameSession classes.

Because INotifyPropertyChanged is an interface, that means the class needs to implement the interface – have the properties, events, and functions defined in the interface. A class can implement multiple interfaces. To do that, we would have added a comma after INotifyPropertyChanged and listed another interface to implement.

Now, we have “BaseNotificationClass” available, which is a class – not an interface.

We will have the Player and GameSession classes “inherit” from BaseNotificationClass.

With inheritance, we would say that “Player” is a “child” of “BaseNotificationClass” – or that “BaseNotificationClass” is the “parent” of “Player”. A class can only inherit from one class in C# – it can only have one “parent”. However, a parent class can have its own parent. You can have many “generations” of inheritance. Although, you don’t really want to use too many. It can become difficult to manage.

A child class has access to the events, properties, and functions in its parent class – if they are public, internal, or protected. So, Player and GameSession use the PropertyChanged event and OnPropertyChanged function in BaseNotificationClass, instead of having their own.

BaseNotificationClass, and its "child" classes - Player and GameSession


To have the Player and GameSession classes inherit from BaseNotificationClass, replace the “INotifyPropertyChanged” with “BaseNotificationClass”. Then, remove the PropertyChanged event and the OnPropertyChanged function.





When we create more classes (Models and ViewModels) that need to notify the UI of changes, they will also inherit from BaseNotificationClass.


Return to main page

10 thoughts on “Lesson 04.4: Improving the World – Inheritance and INotifyPropertyChanged

  1. Excellent leasons! This is the best tutorial I’ve seen to date, that makes it both fun and interesting to practice and hone your C# skills.

    I have an issue with the fonts in the descriptions of the locations. My XAML code looks exactly like yours, but while everything looks the same, the text of my descriptions only takes a single line, even if it’s set to Auto.

    Which is to say, the text is centered, which is fine, but it disappears from left to right, i.e. the sentence “There are rows of corn growing here, with giant rats hiding between them” doesn’t show completely, ‘cos it takes 1 lines insted of 2 or more. FontSize is set at 11pt.

    Keep up the good work, Scott! And thanks for this. Best wishes

      1. Thanks Scott! No it doesn’t. I followed your videos til lesson 4.5, for now, sometimes rewinding or going to a previous lesson, but I haven’t seen it.

        edit: Tested it and it worked like a charm. I am so thankful! Best regards!

  2. Hi Scott ,

    Very interesting guide so far !
    I was just wondering in the video at 2:26 you copy the “nameof”
    and it automatically changed the whole ‘block’.

    Could you tell me how to do this ?
    Thanks in advance.

    1. In the video, I already had the correct code in a separate text file. I had copied the new code into the Windows clipboard buffer (with CTRL-C), and just pasted it in over the old code (with CTRL-V). Unfortunately, there isn’t anything from Visual Studio or ReSharper to make the changes automatically.

      1. I’m using FXCop (Microsoft Code Analysis) that scans your code for potential improvements, errors, SQL Injections,…
        With this tool, it not only tells you that “nameof” is better than a string but also offers to autofix all occurences

Leave a Reply

Your email address will not be published. Required fields are marked *