Get All Indexes of an Item in an Array

Following example returns the list of index for a string in an array

using System;
using System.Linq;

namespace ZeroToCSharp {
    class Program {
        static void Main(string[] args) {
            string[] cars = { "Renault", "Volvo", "BMW", "Volvo", "Mazda", "BMW", "BMW", "Ford" };
            var result = Enumerable.Range(0, cars.Length).Where(i => cars[i] == "BMW").ToList();

            //To print output
            result.ForEach(x => Console.WriteLine(x));
            Console.ReadKey();
        }
    }
}

Output:

2
5
6

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x