There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
var result = heroes.Select(h => $"Introducing...{h}!");
the solution I came up with looks as follows:
var my = heroes.Select(h => heroes[Array.IndexOf(heroes, h)] = $"Introducing...{h}!");
My question is, how can => replace the location of where I want the array to be edited: heroes[Array.IndexOf(heroes, h)] =
I checked the cheatsheet for Lambda expressions, but I couldn’t find any indication as to the logic behind this, so I don’t think I could reproduce the kind of thinking necessary to come up with the shorter version. I lack some kind of knowledge, I think.
Hi,
so the lambda expression represents each value in a collection. That is the h => h part. You can literally read this as: for each element, that I’m gonna name h, do this…
This is because your var my is actually an IEnumerable of a type string. IEnumerables are a type of collection. Similar but also different from lists. Select() is a method you get when you include LINQ in your namespace. Under the hood, this method ITERATES. It will check and/or do something for every value in the same collection.
So unless you give a condition with Where() it will do this for ALL elements.
So, on the other hand, if you only wanted to give the new introduction to only some of the heroes, not all, you would have to use a chained statement with Where() where you would specify with a condition which heroes would be affected by your action.
The format requested is “Introducing…[HERO NAME]!” which I took literally and put the hero names into brackets and made them capital letters, but that marks the printed result as incorrect for this module. I looked at the solution and realized they wanted “Introducing…Hero Name!” with no brackets and not modified to have capital letters. I wish that were made clear.
Same. I wasted a lot of time trying to figure out what was wrong with the way I had coded it due to that UPPER CASE of HERO NAME - instructions should show an actual example to avoid this confusion/ frustration. I tried with/without the square brackets, with/without spaces etc. before finally looking at the solution.
First off you have to get each individual list for each foreach loop. Second you are trying to run another foreach loop in your other foreach loop and that means you are trying to take each individual value of a list as a list, and since individual values aren’t lists themself that doesn’t work. or:
foreach (var heroVar in { heroes2, heroes3 }) //you are trying to run a list of two values and get them individually
{
foreach (var hero in heroVar) //you are trying to take each of those individual values as a list, which they aren’t //error
{
Console.WriteLine(hero);
}
}
I suggest:
string heroes = { heroes2, heroes3 }; //you should set up a list instead of making a probably unusable list in foreach loop
//you might also need to reassign another valuable with lambada expression to use
foreach (var heroVar in heroes ) //this will print each one
{
Console.WriteLine(heroVar);
}