我想在包含我在C#中的值的数组中获取索引 [英] I want to get index in array which contains my value in C#
本文介绍了我想在包含我在C#中的值的数组中获取索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在包含我在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};
在此示例中,我将获得值11的索引。数组类型为Byte。
I will get index of value 11 for this example. The array type is Byte.
推荐答案
您可以使用 IndexOf 方法:
int[] array = { 2, 3, 5, 7, 11, 13 };
int index = Array.IndexOf(array, 11); // returns 4
或带有字节数组:
byte[] primes = { 2, 3, 5, 7, 11, 13 };
int index = Array.IndexOf(array, (byte)11); // returns 4
这篇关于我想在包含我在C#中的值的数组中获取索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文