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 trialAmy Shah
25,337 PointsProblem with understanding how the public static bool method is supposed to work here
Now, create a public static method inside the ContainsAnyExtension named ContainsAny that returns a bool - since this is an extension method, the first parameter of the method should be a string preceded by the this keyword, named source. The second parameter should be an IEnumerable<string> named stringsToMatch. Inside the method, use a LINQ query to identify if any of the stringsToMatch are found as partial matches in the source string. Be sure to return true if at least one of the strings in stringsToMatch are found. The search should be case sensitive.
I have attempted to follow the directions but I know for sure that i could use a suggestion about how to return true if one of the strings are found. I did not have a good attempt for that part
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 (true);
}
}
}
2 Answers
Chanuka Weerasinghe
2,337 PointsHIya,
You need the body context
i.e. .stringsToMatch.Contains(source)
meaning this -
if(yourList.Contains("your name"))
{ //Do Some thing }
.Contains is by default case sensitive and you should use ToLower if you need to get a word match.
i.e. .Contains(yourList.ToLower("Your Name"))
LINQ is quite simple once you get to know the basic try this https://www.tutorialspoint.com/linq/linq_query_operators.htm "Contains" comes under Quantifier Operations
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.