Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to get index in array which contains my value in C# [duplicate]

Tags:

arrays

c#

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.

like image 959
rbagheri Avatar asked Jun 11 '11 13:06

rbagheri


People also ask

How do you get the index of value C from an array?

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.

How do you display the index of an element in an array?

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.

How do you find the index of value in an array of objects?

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.


1 Answers

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        
like image 184
Darin Dimitrov Avatar answered Oct 04 '22 15:10

Darin Dimitrov