在" foreach"循环中获取数组键
时间:2020-03-05 18:52:33 来源:igfitidea点击:
如何在C#的foreach
循环中获取当前元素的键?
例如:
的PHP
foreach ($array as $key => $value) { echo("$value is assigned to key: $key"); }
我想在C#中做的事情:
int[] values = { 5, 14, 29, 49, 99, 150, 999 }; foreach (int val in values) { if(search <= val && !stop) { // Set key to a variable } }
解决方案
回答
las,没有内置的方法可以做到这一点。使用for循环或者创建在每次通过时递增的临时变量。
回答
myKey = Array.IndexOf(values,val);`
回答
如果要获取键(读取:索引),则必须使用for循环。如果我们实际上想拥有一个包含键/值的集合,那么我会考虑使用HashTable或者Dictionary(如果我们想使用泛型)。
Dictionary<int, string> items = new Dictionary<int, string>(); foreach (int key in items.Keys) { Console.WriteLine("Key: {0} has value: {1}", key, items[key]); }
希望能有所帮助,
泰勒
回答
实际上,如果要遍历数组,则应使用经典的(;;)循环。但是,用C语言通过Dictionary可以实现用PHP代码实现的类似功能:
Dictionary<int, int> values = new Dictionary<int, int>(); values[0] = 5; values[1] = 14; values[2] = 29; values[3] = 49; // whatever... foreach (int key in values.Keys) { Console.WriteLine("{0} is assigned to key: {1}", values[key], key); }
回答
Grauenwolf的方法是使用数组执行此操作的最直接,最高效的方法:
Either use a for loop or create a temp variable that you increment on each pass.
哪一个看起来像这样:
int[] values = { 5, 14, 29, 49, 99, 150, 999 }; for (int key = 0; key < values.Length; ++key) if (search <= values[key] && !stop) { // set key to a variable }
使用.NET 3.5,我们还可以采用一种更具功能性的方法,但是该方法在站点上有些冗长,并且可能依赖于几个支持函数来访问IEnumerable中的元素。如果这只是我们所需要的,那就矫kill过正,但是如果我们倾向于进行大量的收集处理,则很方便。
回答
我在这个问题的另一个版本中回答了这个问题:
Foreach is for iterating over collections that implement IEnumerable. It does this by calling GetEnumerator on the collection, which will return an Enumerator. This Enumerator has a method and a property: * MoveNext() * Current Current returns the object that Enumerator is currently on, MoveNext updates Current to the next object. Obviously, the concept of an index is foreign to the concept of enumeration, and cannot be done. Because of that, most collections are able to be traversed using an indexer and the for loop construct. I greatly prefer using a for loop in this situation compared to tracking the index with a local variable.
如何获取foreach循环当前迭代的索引?
回答
这是我刚想出的解决方案
原始代码:
int index=0; foreach (var item in enumerable) { blah(item, index); // some code that depends on the index index++; }
更新的代码
enumerable.ForEach((item, index) => blah(item, index));
扩展方式:
public static IEnumerable<T> ForEach<T>(this IEnumerable<T> enumerable, Action<T, int> action) { var unit = new Unit(); // unit is a new type from the reactive framework (http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx) to represent a void, since in C# you can't return a void enumerable.Select((item, i) => { action(item, i); return unit; }).ToList(); return pSource; }
回答
我们可以使用扩展方法自己实现此功能。例如,这是扩展方法KeyValuePairs的实现,该方法可在列表上使用:
public struct IndexValue<T> { public int Index {get; private set;} public T Value {get; private set;} public IndexValue(int index, T value) : this() { this.Index = index; this.Value = value; } } public static class EnumExtension { public static IEnumerable<IndexValue<T>> KeyValuePairs<T>(this IList<T> list) { for (int i = 0; i < list.Count; i++) yield return new IndexValue<T>(i, list[i]); } }