Exercises
As a new C# coder, it might take you some time to recognize when to use interfaces.
To help overcome this, let’s consider a common occurrence — sorting a
List
of objects.
If the list contains
string
or numerical entries, then sorting the list is trivial:listName.Sort();
However, if the elements are custom objects (like
Cat
), then sorting the list becomes more complicated. This is because the objects may contain multiple fields, any of which could be used as a sorting option. ForCat
objects, this could includename
,age
, ormass
.
Getting Started
Open up your exercises starter code repo in Visual Studio to get started!
You will practice implementing interfaces by playing around with a small ice
cream store. It consists of a refrigerated display Case
, which contains
a collection of ice cream Flavor
objects and a selection of Cone
objects.
Did you notice the abstract Ingredient
class? This gets extended by
Flavor
and Cone
to help streamline the code.
Sorting Flavors by Name
To display a menu for your customers, you need to sort the ice cream flavors
alphabetically by the name
field. Fortunately, the IComparer
interface helps you solve the sorting-objects-by-field problem.
Before proceeding, make sure you have read the section on the IComparer interface !
Create a Sorting Class
Create a new class called
FlavorComparer
and have it implement theIComparer
interface:public class FlavorComparer : IComparer<Flavor>
To start sorting, we need a
Compare()
method. Add the following code to create one:1 2 3 4
public int Compare(Flavor x, Flavor y) { return string.Compare(x.Name, y.Name); }
This returns an integer (-1, 1, or 0) depending on which
Flavor
objectx
ory
comes first, alphabetically.
Sorting the Flavors
List
In Program.cs
, we declare menu
that contains everything in the Case
as well as specific availableFlavors
and availableCones
collections.
|
|
To sort the
availableFlavors
list, first create a newFlavorComparer
object.13
FlavorComparer comparer = new FlavorComparer();
Next, call the
Sort
method onavailableFlavors
and pass thecomparer
object as the argument.15
availableFlavors.Sort(comparer);
Iterating through the
availableFlavors
list with a loop before and after the sort shows the results. (The output below displays just thename
fields).Before: After: Vanilla Chocolate Chocolate Red Velvet Red Velvet Rocky Road Rocky Road Strawberry Sorbet Strawberry Sorbet Vanilla
Instead of declaring and initializing the comparer
object, we could
combine steps 1 and 2 by using a single statement:
availableFlavors.Sort(new FlavorComparer());
Sorting Cones by Cost
Now let’s sort our availableCones
list by cost, from least expensive to most
expensive.
Create the new class
ConeComparer
.Follow the example above to implement the
IComparer
interface and evaluateCone
objects by cost. Since comparing two numbers is different from comparing strings, try getting the difference between the two numbers. If the difference is positive, then we know the first number is greater. If the difference is negative, then we know that the second number is greater.In the
Main()
method, sort theavailableCones
list, then print the elements to the screen to verify the results.Before: After: Waffle: $1.25 Bowl: $0.05 Sugar: $0.75 Wafer: $0.50 Wafer: $0.50 Sugar: $0.75 Bowl: $0.05 Waffle: $1.25
Remember that the cost
field is of type double
and Compare()
has a return type of type int
!
Bonus Mission
Modify FlavorComparer
to sort Flavor
objects by the number of allergens, from lowest to highest.
Next Steps
In these exercises, you practiced implementing existing interfaces. In the studio activity, you will design and implement your own.