Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
In this video, we'll introduce our first challenge—replacing the fields with properties in our Media Library program's media type classes.
Prerequisites
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.
Instructions
1st Challenge
-
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
Loanee
andOnLoan
properties inline to an empty string (i.e.""
orstring.Empty
) andfalse
respectively.
C# Property Syntax
If you need a refresher on either the "standard" or "auto-implemented" property syntax in C#, checkout the following documentation.
Help
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
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up