Reputation: 199
Below are two extension methods.
ContainsAny() will check if any of the string-elements in the IEnumerable contain the string that the method is called on. OK, No problem
_ContainsAny() uses a chained Linq expression to check if any of the string-elements in the IEnumerable are contained in the string.
1) First question is, is there a differnce at all in the result that it can produce following these two diffent type of logic. I get the same results.
2) Second question is, How does the Chain Linq expression working here?
return stringsToMatch.Any(item => source.Contains(item));
Contains() returns a boolean to the Any() method?
How exactly are the Any() and Contains() methods working together in this chain expression?
public static class ContainsAnyExtension
{
public static bool ContainsAny(this string source, IEnumerable<string> stringsToMatch)
{
return stringsToMatch.Contains(source);
}
public static bool _ContainsAny(this string source, IEnumerable<string> stringsToMatch)
{
return stringsToMatch.Any(item => source.Contains(item));
}
}
Upvotes: 0
Views: 930
Reputation: 887
They are two different functions :
public static bool ContainsAny(this string source, IEnumerable<string> stringsToMatch)
{
return stringsToMatch.Contains(source);
}
The first one is an extension method defined in LINQ. It checks the list stringsToMatch
to see if one of these strings is equal to your source
. As in, it is comparing strings to strings one to one.
public static bool _ContainsAny(this string source, IEnumerable<string> stringsToMatch)
{
return stringsToMatch.Any(item => source.Contains(item));
}
The second one is an instance method of string
. What source.Contains(item)
does is that, to quote from documentation, "Returns a value indicating whether a specified substring occurs within this string." In this case it checks if source
contains the substring item
.
What Any()
does is that it returns true
or false
whether any item in the enumerable returns true
for the lambda function provided. Instead of putting the Lambda function you can define an anonymous method and it would just work the same:
return stringsToMatch.Any(delegate (string item) { return source.Contains(item); });
You can probably see why the lambda function is used. It just looks plain nicer.
Edit: I was playing around with it in Visual Studio and apparently this is valid code too. Hah !! I have no idea if it would work or not.
return stringsToMatch.Any(source.Contains);
To see the definition of both methods, right click them in Visual Studio and choose "Go to Definition" or "Peek definition". That will take you to the definition and comments for both of them.
To see the difference yourself, try it with a data set that where source
isn't in stringstoMatch
but some of the strings in stringsToMatch
are substrings of source
. You'll see the discrepancy right away. For example: "abc"
is a substring of "xyzabcdef"
.
Edit: Here I tried it in C# interactive:
> var list = new List<string> { "abc", "def", "xyz" };
> var x = "abcd";
> x.ContainsAny(list)
false
> x._ContainsAny(list)
true
You can see the difference between the two methods now.
Upvotes: 0
Reputation: 2031
Answering your questions:
First, they are not the same at all, the first one is checking if the list has the string as its as an element, while the second one checks each elements if it has the string as part of it.
1) First question is, is there a differnce at all in the result that it can produce following these two diffent type of logic. I get the same results.
Yes, the first will return only if the list contains the element (true/false) The second one will return if any of the elements has the string cotnained IN IT.
2) Second question is, How does the Chain Linq expression working here? return stringsToMatch.Any(item => source.Contains(item)); Contains() returns a boolean to the Any() method? How exactly are the Any() and Contains() methods working together in this chain expression?
This is not a chain expression, its just a boolean function added as extention.
Upvotes: 1