How to search for multidimensional array?

In C #,

Array.Find<T>(arrayName, value); 

looking for a one-dimensional array. Is there a way to do this for a multidimensional array (for example, myArray[,,] )?

+4
source share
3 answers

Working with Excel and VSTO, I always work with multidimensional arrays. For a multidimensional array, such as Array.Find (), there are no built-in functions.

You basically have two options: create your own helper methods and implement a common search pattern there, or create a list of domain objects corresponding to the contents of a multidimensional array. I personally had a tendency to choose the last option.

If you decide to write a helper method, it might look something like this (something like this):

 // you could easily modify this code to handle 3D arrays, etc. public static class ArrayHelper { public static object FindInDimensions(this object[,] target, object searchTerm) { object result = null; var rowLowerLimit = target.GetLowerBound(0); var rowUpperLimit = target.GetUpperBound(0); var colLowerLimit = target.GetLowerBound(1); var colUpperLimit = target.GetUpperBound(1); for (int row = rowLowerLimit; row < rowUpperLimit; row++) { for (int col = colLowerLimit; col < colUpperLimit; col++) { // you could do the search here... } } return result; } } 

You are referring to a static extension like this in other parts of your application code:

 object[,] myArray = GetMyArray(); // gets an array[,] myArray.FindInDimensions(someObject); 
+6
source

There is no built-in multidimensional search function. You have to write it yourself.

+3
source

Flatten the multidimensional array, and then use Array.Find .

+2
source

All Articles