Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Using a `for` Loop4:13 with James Churchill
In this video, we'll introduce our first challenge—using a `for` loop to loop through our Media Library items and output each item's information to the console.
This practice session assumes that you've completed stage 6 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 five practice sessions that cover stages 1-5 of the C# Objects course.
- Practice Creating Classes in C#
- Practice Methods in C#
- Practice Inheritance in C#
- Practice Encapsulation and Arrays in C#
- Practice Properties in C#
If you haven't completed these practice sessions yet, you might consider doing that first before attempting this practice session.
Add a method named
DisplayItems()to the MediaLibrary class.
- Use a
forloop to loop through the items array contained within the MediaLibrary class.
- Cast each item to its subtype and access its
DisplayTextproperty, passing the property's value to a
- Use a
In the Program.cs file, replace the calls to the
Display()method with a single call to the
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.
This is James.
In this C# practice session,
you'll practice using for and foreach loops in C#.
It reinforces what you've learned in stage six of this 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 sixth 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,
arrays, and properties to improve the design of our program.
In this practice session, we'll see how we can loop through our array
of media type items using two different types of loops.
The for and for each loops.
In an earlier practice session, we covered a third type of loop, while loops.
See the teacher's notes for a link to that practice session.
Go ahead and open your workspace from the previous practice session in this series.
Or you can open the workspace 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.
If you open the program.cs file, you can see here in the main method,
where we're instantiating an instance of our media library class.
In passing in an array containing instances of our media type subclasses.
Our rate of media type items contains three albums, a book, and a movie.
After creating and initializing the media library instance,
we call the DetectMediaType helper method on each item in our
library in order to detect and output its subtype to the console.
Then, we called the display helper method on each item,
in order to display its information to the console.
The display method uses the C# isoperator to determine each media
type's subtype in order to explicitly cast the item to that subtype.
Casting to the subtype gives us access to the sub-classes display text property.
Listing our media library items to the console is currently a very
We have to manually add a line of code for each new item that we add to our library.
Let's improve our program by adding a method to the media library class,
that when called, will loop through the available items and
output each item's information to the console.
For your first challenge,
add a method named DisplayItems to the media library class.
Use a for loop to loop through the items array contained within the media
As you loop through the media library items,
you'll cast each item to its subtype and access its display text property.
Passing the property's value to a console right line method call.
Then, in the Program.cs file,
replace the calls to the'Display () method with
a single call to the DisplayItems () method.
And that's your first challenge.
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