Replacing Fields with Properties3:24 with James Churchill
In this video, we'll introduce our first challenge—replacing the fields with properties in our Media Library program's media type classes.
This practice session assumes that you've completed stage 5 of the C# Objects course. If you haven't done that yet, go check it out, then come back to this workshop.
This practice session also builds upon the skills that are covered in these four practice sessions that cover stages 1-4 of the C# Objects course.
- Practice Creating Classes in C#
- Practice Methods in C#
- Practice Inheritance in C#
- Practice Encapsulation and Arrays in C#
If you haven't completed these practice sessions yet, you might consider doing that first before attempting this practice session.
In the MediaType base class and Album, Book, and Movie subclasses, replace each public class field with a property.
- Use either the regular C# property syntax or the more concise auto-implemented property syntax.
- Use private setters for fields that should be readonly and initialize those properties via the constructor.
In the MediaType base class, initialize the
OnLoanproperties inline to an empty string (i.e.
C# Property Syntax
If you need a refresher on either the "standard" or "auto-implemented" property syntax in C#, checkout the following documentation.
If you get stuck on any of the following topics or simply need a refresher, click on a topic in list below to view the associated video in the C# Objects course.
- Recall that accessor methods are public methods that can be used to get or set the value of a private field
- Recall that a "getter" method is a method to get the value of a private field
- Recall that a "setter" method is a method to set the value of a private field
- Write a property for a private backing field
- Use dot notation to access a property
- Add an access modifier to a property's getter or setter
- Write an auto-property
- Initialize a property with a private setter in the constructor of the class
- Initialize a property inline with its declaration
You need to sign up for Treehouse in order to download course files.Sign up