【发布时间】:2011-06-11 13:26:10
【问题描述】:
我想在包含我在 C# 中的值的数组中获取索引。 例如, 我的数组是:
byte[] primes = {2, 3, 5, 7, 11, 13};
对于这个例子,我将得到值 11 的索引。数组类型为Byte。
【问题讨论】:
我想在包含我在 C# 中的值的数组中获取索引。 例如, 我的数组是:
byte[] primes = {2, 3, 5, 7, 11, 13};
对于这个例子,我将得到值 11 的索引。数组类型为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
【讨论】: