Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# List of Integer Arrays does not Contain Item

My goal is to add an unknown number of integer coordinates to a collection. While I can add these coordinates to this list List<int[]> coordList = new List<int[]>(); I cannot check if coordList.Contains(specifiedCoordinate).

This is what I have so far:

List<int[]> coordList = new List<int[]>();
coordList.Add(new int[] {1, 3});
coordList.Add(new int[] {3, 6});
bool contains = coordList.Contains(new int[]{1, 3})
Console.WriteLine(contains);

However, contains is always false even though I specify the same values that I add.
I have tried ArrayList as a possible alternative, but the results are the same as using List.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If there’s something I’m not understanding or if there’s an alternative, I’m all ears.

Thank you!

>Solution :

It seems like you want:

bool contains = coordList.Any(a => a.SequenceEqual(new int[]{1, 3}));

SequenceEqual docs.

.Any and .SequenceEqual are extension methods provided by the System.Linq namespace. You may need to ensure that you have using System.Linq; at the top of your code file to make this work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading