C# .NET

C# Foreach Index, Value

Code GGOON 2020. 10. 8. 17:56
반응형

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;
}
반응형