- Lambda expressions and anonymous methods replace the specified single-use delegate handlers.
OnBalanceChange delegate using a lambda expression, as shown in the code below.Then remove the HandleBalanceChanged method.
this.moneyPocket.OnBalanceChange = () =>
{
this.OnBalanceChange?.Invoke();
};OnTemperatureChange delegate using a lambda expression that does what the zoo's HandleBirthingRoomTemperatureChange method does.Then remove the HandleBirthingRoomTemperatureChange method.
The previousTemp and currentTemp mean that those are the two parameters needed. The types of these parameters are assumed.
this.b168.OnTemperatureChange = (previousTemp, currentTemp) =>
{
// HandleBirthingRoomTemperatureChange code goes here
};CageWindow's constructor, attach the content of the Update method to the cage's OnImageUpdate delegate using a lambda expression, as shown in the code below that does what the Update method does.Then remove the Update method.
this.cage.OnImageUpdate = item =>
{
// code here
};AttachDelegates methods, attach the content of the following methods to the appropriate delegates.Then remove the unused methods.
HandleAnimalAddedHandleAnimalRemovedHandleGuestAddedHandleGuestRemovedHandleBirthingRoomTemperatureChanged
Animal class called OnPregnant.It will be used to alert the zoo when an animal becomes pregnant.
The anonymous method adds that animal to the birthing room's queue.
animal.OnPregnant = pregnantAnimal => this.b168.PregnantAnimals.Enqueue(pregnantAnimal);animal.OnPregnant = (pregnantAnimal) =>
{
this.b168.PregnantAnimals.Enqueue(pregnantAnimal);
};Animal class, call the OnPregnant delegate in the MakePregnant method.AddAnimal method.Handle animals that have been added through a list initializer by plugging the same code/anonymous method in to each animal's pregnancy delegate in the zoo's OnDeserialized method (within the foreach loop).
Run the WPF application.
Then edit the animal and make it pregnant. The breakpoint should be hit, which means that the method was attached to the delegate correctly.
The pregnant animal should give birth to a baby animal and become not pregnant. The baby animal should be added to the list box.
ForEach performs the specified operations on collection items.In this task, you will replace existing simple foreach loops (ones containing only a single line of code in the loop) with calls to the ForEach and ToList extension methods and lambda expressions. You will check your work by ensuring that the functionality of the WPF and console applications remain the same as before refactoring the loops.
ListUtil's Flatten method, replace the foreach loop with a call to the ForEach extension method.The ForEach method is only available on lists, so you will have to call the ToList method before calling the ForEach method.
list.ToList().ForEach(s => result += result == null ? s : separator + s);ConsoleUtil's WriteHelpDetail method with three parameters, replace the foreach loop with a call to the ToList and ForEach extension methods.CageWindow's DrawAllItems method, replace the foreach loop with a call to the ToList and ForEach extension methods.Increment the zIndex variable when passing it in to the DrawItem method instead of after calling the method.
c => this.DrawItem(c, zIndex++)OnDeserialized methodForEach extension method.this.guests.ForEach(g =>
{
this.OnAddGuest(g);
g.GetVendingMachine += this.ProvideVendingMachine;
});Since this foreach loop has more than one line of code in it, we need to add curly braces to signify that the code applies to all objects. Structure:
ForEach(param => { //code here });ForEach extension method.AddAnimalsToZoo method in the Zoo class, use a ForEach extension method with curly braces to execute all code in the foreach loop.Open their cage. All animals should still be drawn correctly, and they should not flip back and forth between the background and foreground.
Create two guests. Stop and restart the application. The guests should be deserialized successfully and should appear in the list box.
The information, including the usage and parameter details, should be written to the console as they were before the change.
In this task, you will practice using lambda expressions and extension methods built into the .NET framework by writing queries to find specific animals and guests and finding information about all animals in the zoo. You will check your work by running the queries and ensuring that you get the correct result.
6.4.5 How to Write and Use LINQ QueriesConsoleHelper called QueryHelper.
"query" on the Program's switch statement.Inside the case statement, assign a local string variable to a call to the QueryHelper method. Pass in the zoo and the second command in the commandWords array.
QueryHelper method, create a local string variable with a message stating that something went wrong.Then add a switch statement that switches on the query parameter.
"totalanimalweight", "averageanimalweight", and "animalcount".Sum extension method.Do not use the TotalAnimalWeight property.
result = zoo.Animals.ToList().Sum(a => a.Weight).ToString();Do not use the AverageAnimalWeight property.
Do not use the AnimalCount property.
"object"."firstheavyanimal", "firstyoungguest", and "firstfemaledingo".Then, use the FindAnimal method to find the first animal whose weight is over 200 pounds and add that animal to the list. Then, use the ForEach extension method to set the resulting animal the local string variable.
Then find the first guest who is 10 years old or younger and add that guest to the list. Use the ForEach extension method to set the resulting guest to the local string variable.
Then find the first animal who is both a dingo and is female and add that animal to the list. Use the ForEach extension method to set the resulting animal the local string variable.
"query totalanimalweight". The result should be 2687.95."query averageanimalweight". The result should be 127.9976..."query animalcount". The result should be 21."query firstheavyanimal".A representation of an ostrich named Stretch should appear in the data grid.
"firstyoungguest".A representation of Darla who is under 10 should appear in the data grid.
"query firstfemaledingo".A representation of the dingo named Maggie should appear in the data grid.
Animals should move and grow tired normally like before.Zoo classOnDeserialized methodAddAnimalsToZoo methodConsoleHelper classQueryHelper method© 2026 Northcentral Technical College