Click here to Skip to main content
15,902,445 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to test whether an element equals an array. However, even if array==arraylist[element] the value of repetitive is still set to false.

Here is my code:

bool repetitive = false;
foreach(var element in arraylist)
{
if (array.Equals(element))
repetitive = true;
}
return repetitive;
Posted

1 solution

Never use ArrayList in new development. All non-generic classes were rendered obsolete as early as of .NET v.2.0, when generics were implemented. They eliminate unwanted potentially dangerous element type casting. Use generic System.Collections.Generic.List<> and other generic collection.

Your code makes no sense at all, you compare some element with the whole array (if your array object is array; it is undeclared), which always be false. Moreover, System.Equals is not intended for direct call; its purpose is very different.

It's totally unclear what you are trying to achieve (if it even makes any sense at all; as is, it won't even compile because array is not declared and defined), but the closest approach you would need is this one of Any or All extension methods described here:
http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900