How do you get the index of the current iteration of a foreach loop?

后端 未结 30 1679
刺人心
刺人心 2020-11-22 07:05

Is there some rare language construct I haven\'t encountered (like the few I\'ve learned recently, some on Stack Overflow) in C# to get a value representing the current iter

30条回答
  •  抹茶落季
    2020-11-22 07:42

    Unless your collection can return the index of the object via some method, the only way is to use a counter like in your example.

    However, when working with indexes, the only reasonable answer to the problem is to use a for loop. Anything else introduces code complexity, not to mention time and space complexity.

提交回复
热议问题