Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
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
Hi, there this is James, in this C# practice session,
you'll practice creating and using properties in C#.
It reinforces what you learned in stage 5 of the C# objects course.
If you find this practice session too challenging to complete,
you might need to review that course.
See the teacher's notes for a link.
This practice session is the fifth in a series of sessions where you'll build out
a media library console application.
Step by step, you'll add features to the program.
Eventually you'll be able to use C# to add, list, and
search for items like albums, books, and movies,
whatever you want to have cataloged in your media library.
In the previous practice sessions we created a class hierarchy for
our program's media types and
we used encapsulation and arrays to improve the design of our program.
In this practice session we'll see how we can replace class fields with properties.
And use computer properties to add atributes whose values
are based upon other atributes.
Go ahead and open your work space from the previous practice session, or
you can open the work space that I´ve attached to this video.
If you want, you can download the project files in
order to use an external editor or IDE like Visual Studio.
Before we go about replacing our class fields to properties,
let's reveal what we'd want to do this?
In the C# objects course, three reasons were discussed.
First, properties have getters and setters which gives us a way to
associate code with the get in and or setting of the property's value.
Second, properties make debugging easier as you can set
break points in gitters or setters.
And third, properties help future proof our code by encapsulating our fields.
This gives us a way to change the underlying implementation of
our class without changing how consumers interact with it.
Our media library program contains four media type related classes.
A Media Type base class, and
three Media Type subclasses—album,
book, and movie.
For your first challenge, replace each public field with a property,
in the MediaType base class, and Album, Book, and Movie subclasses.
Use either the regular C# property syntax, or
the more concise auto-implemented property syntax.
Also use private setters for fields that should be read-only and
initialize those properties via the constructor.
And, in the media type base class, initialize the loanee and
onloan properties and line to an empty string and false, respectively.
And that's your first challenge.
Good luck, and we'll see you in the next video,
where I'll walk through my solution.
You need to sign up for Treehouse in order to download course files.Sign up