Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDean Wiseman
20,901 PointsExtension Method
I can't seem to make this work. I've spent way to long on this. Going to need some help.
using System.Collections.Generic;
using System.Linq;
namespace Treehouse.CodeChallenges
{
public static class ContainsAnyExtension
{
public static bool ContainsAny(this string source, IEnumerable<string> stringsToMatch)
{
return stringsToMatch.Any(ThisElement => ThisElement.Contains(source));
}
}
}
2 Answers
Sam Sycamore
13,596 PointsIf you switch the second "ThisElement" with the last "source" in this line,
{
return stringsToMatch.Any(ThisElement => ThisElement.Contains(source));
}
You'll have completed it. Hopefully it makes sense as to why :)
{
return stringsToMatch.Any(ThisElement => source.Contains(ThisElement));
}
Kevin Gates
15,053 PointsAfter a lot of researching, I believe this is the simplest answer that is in accordance with what they were expecting:
using System.Collections.Generic;
using System.Linq;
namespace Treehouse.CodeChallenges
{
public static class ContainsAnyExtension
{
public static bool ContainsAny(this string source, IEnumerable<string> stringsToMatch)
{
return stringsToMatch.Any( strings => source.Contains(strings));
}
}
}
Essentially the return statement returns a boolean (true / false).
First, the stringsToMatch.Any(...) is looking for anything in it's IEnumerable "List" to return true, and if so, the .Any(...) also returns true to the Return statement -- meaning one of the words passed in by the user was matched with one of our strings.
Second the ** strings => source.Contains(strings) ** is passing in the variable "strings" (which is the parameter data that is an item in the IEnumerable "List"). The function has us check to see if our source parameter contains any of the strings we're checking against. If it's False, it loops to the next item in the IEnumerable "List", else it returns True then it fulfills the Any function, which means, our Contains Any method is returning True also.
Dean Wiseman
20,901 PointsDean Wiseman
20,901 PointsI really over-thought this one. I don't know how I didn't see that. Need to slow down I think. Thanks Sam.