I want to get index in array which contains my value in C#. For example, my array is:
byte[] primes = {2, 3, 5, 7, 11, 13};
I will get index of value 11 for this example. The array type is Byte.
To find the index of specified element in given Array in C programming, iterate over the elements of array, and during each iteration check if this element is equal to the element we are searching for.
In order to find the index of an element Stream package provides utility, IntStream. Using the length of an array we can get an IntStream of array indices from 0 to n-1, where n is the length of an array.
To find the index of an object in an array, by a specific property: Use the map() method to iterate over the array, returning only the value of the relevant property. Call the indexOf() method on the returned from map array. The indexOf method returns the index of the first occurrence of a value in an array.
You could use the IndexOf method:
int[] array = { 2, 3, 5, 7, 11, 13 };
int index = Array.IndexOf(array, 11); // returns 4
or with a byte array:
byte[] primes = { 2, 3, 5, 7, 11, 13 };
int index = Array.IndexOf(array, (byte)11); // returns 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With