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