4

Background:

I'm working on an evaluator (I know there's solutions available, but I need some features that I need to implement myself). I need to find all occurrences of open brackets in the evaluation. However, for that I need all the indexes of the brackets.

Question:

Is there something like an AllIndexesOf method that returns a int[], or IEnumerable<int>?

It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103

2 Answers2

8

There is not but you can get all the indexes using the following LINQ query.

int number  = 10;
int[] intArray = new[] { 1, 32, 10, 5, 65, 6, 10, 10 };
var allIndexes = intArray.Select((r,i)=> new {value = r, index = i})
                         .Where(r=> r.value == number)
                         .Select(r=> r.index);

allIndexes will contain 2,6 and 7

Habib
  • 219,104
  • 29
  • 407
  • 436
4

You also can use Enumerable.Range

 var indexes = Enumerable.Range(0, list.Count)
                         .Where(index => list[index] == yourValue);
cuongle
  • 74,024
  • 28
  • 151
  • 206