Reputation: 33
I've realized that in some old code that I've written in C#, when comparing the occurrence of a string in an array that I've done it like so:
for (var i = 0; i < NounsA.Count; i++)
{
if (NounsB.Any(s => s.Contains(NounsA[i])))
{
--do something--
Where NounsA and NounsB are arrays of words.
I've now just done this:
for (var i = 0; i < NounsA.Count; i++)
{
if (NounsB.Contains(NounsA[i]))
{
--do something--
I've compared the two and they return different values, so what I'm asking is, what does the first one even do?
Upvotes: 2
Views: 433
Reputation: 117010
Let's try this code:
var NounsA = new List<string>() { "a", "b" };
var NounsB = new List<string>() { "aa", "bb" };
{
for (var i = 0; i < NounsA.Count; i++)
{
if (NounsB.Any(s => s.Contains(NounsA[i])))
{
Console.WriteLine("!");
}
}
}
{
for (var i = 0; i < NounsA.Count; i++)
{
if (NounsB.Contains(NounsA[i]))
{
Console.WriteLine("#");
}
}
}
When I run it I get the following output to the console:
!
!
Both blocks are running through the NounsA
list, one element at a time, and asking the following questions respectively:
NounsB
contain as a substring the current value from NounsA
?NounsB
equal exactly the current value from NounsA
?So in my example the first matches twice as "aa"
contains "a"
and "bb"
contains "b"
, but the second does not match because neither "aa"
nor "bb"
equals either of "a"
or "b"
.
Upvotes: 2