6.5A Zoo Assignment

6.5A Zoo Assignment
Write a custom extension method for collections
Acceptance Criteria
Acceptance criteria
  • Flatten is an IEnumerable<string> extension method and the help command still displays its details correctly.
1.Write a custom extension method for collections
Problem
Solution
6.5.1 How to Create Custom Extension Methods
A Make the Flatten method in the ListUtil class an extension method on objects of type IEnumerable<string> by adding the 'this' keyword before the type of the first parameter.

Now instead of having to pass an IEnumerable of type string to this method we can call the Flatten method ON an IEnumerable of type string.

public static string Flatten(this IEnumerable<string> list, string separator)
B In the ConsoleUtil's WriteHelpDetail method, change how the Flatten method is called so that instead of passing in an IEnumerable<string>, the method is called on the IEnumerable<string>.
Console.WriteLine($"Usage: {upperCommand} {arguments.Keys.Flatten(" ")}");
C Check Your Work
1 Run the console application.
2 Run the help command for the show command.

The show command's details and parameters should appear and the help command should work as it did before.

Write custom extension methods for the zoo
Acceptance Criteria
Acceptance criteria
  • ZooExtensions provides FindAnimal and FindGuest without changing their existing call format.
2.Write custom extension methods for the zoo
Problem
Solution
A Create a static class called ZooExtensions in the Zoos project.
B Move the following methods from the Zoo class to the ZooExtensions class.
  • FindAnimal
  • FindGuest
C Make them extension methods by adding a Zoo type named zoo to the beginning of the parameters and add the 'this' keyword in front of it.
D In the methods, use the zoo parameter to access the public collections.

Add the ToList() extension method to the collections to make them lists.

E Check Your Work
1 Run the console application.
2 Create an animal.

Then show that animal. The show animal command, which uses the FindAnimal extension method, should work as before.

3 Create a guest.

Then show that guest. The show guest command, which uses the FindGuest extension method, should work as before.

Turn sort methods into extension methods
Acceptance Criteria
Acceptance criteria
  • The sort and swap methods operate as extensions and all sort types still work.
3.Turn sort methods into extension methods
Problem
Solution
A In the SortHelper, turn each of the four sort methods and the swap method into extension methods.
B In the Zoo and SortHelper classes (including in the QuickSort method), call each of the methods as extension methods instead methods on the static SortHelper class.
C Check Your Work
1 Run the console application.
2 Sort the animals using each sort type to ensure that the sorting still works as before.
Demonstrate your work
4.Record a video
A Show the places where extension methods are used
1 ConsoleUtil's WriteHelpDetail method
2 Zoo class' SortObjects method
B Explain what an extension method is. Why might you want to create and use one over a regular method?
Prepare and submit your work
5.Submission Steps
A Ensure that your application has no compiler errors or warnings
B Ensure that your code is StyleCop compliant
C Remove all bin and obj folders from your solution
D Zip your solution
E Submit your zipped solution to the Feedback System
F Submit your Feedback System results link to Canvas
G Submit your video recording to your f-channel
Source credit

© 2026 Northcentral Technical College