반응형
Foreach 반복문에서 Index (반복횟수) 사용법
CASE #1.
foreach(var item in somethingList)
{
// IndexOf 메소드를 활용하여 item의 순번을 통한 Index파악
int index = somethingList.IndexOf(item);
}
CASE #2.
foreach (var item in testList.Select((value, index) => new { Value = value, Index = index }))
{
//Get the value through item.Value;
string currentValue = item.Value;
//Get the index through item.Index;
int currentIndex = item.Index;
}
반응형
'C# .NET' 카테고리의 다른 글
[C#] 시작프로그램 등록코드 (0) | 2021.10.28 |
---|---|
[C#] uint, UInt16, UInt32 and UInt64 in C# (0) | 2020.09.12 |
Panel내부에서 Control 위치변경 ( Drag & Drop ) (0) | 2019.12.11 |