c# - Getting the array key in a 'foreach' loop -


how key of current element in foreach loop in c#?

for example:

php

foreach ($array $key => $value) {     echo("$value assigned key: $key"); } 

what i'm trying in c#:

int[] values = { 5, 14, 29, 49, 99, 150, 999 };  foreach (int val in values) {     if(search <= val && !stop)     {          // set key variable     } } 

grauenwolf's way straightforward , performant way of doing array:

either use loop or create temp variable increment on each pass.

which of course this:

int[] values = { 5, 14, 29, 49, 99, 150, 999 };  (int key = 0; key < values.length; ++key)   if (search <= values[key] && !stop)   {     // set key variable   } 

with .net 3.5 can take more functional approach well, little more verbose @ site, , rely on couple support functions visiting elements in ienumerable. overkill if need for, handy if tend lot of collection processing.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -