반복문에서 아이템을 지우는 방법이 여러가지 있는데 많이 쓰이는 뒤에서 부터 읽어서 지우는 방법으로 하면 꽤 괜찮은 코드를 짤 수 있다.
class Program
{
static void Main(string[] args)
{
List<string> items = new List<string>()
{
"최용국",
"이은혜",
"도로시",
"최예윤"
};
Console.WriteLine(string.Join(", ", items));
for (int index = items.Count - 1; index > -1; index--)
{
var item = items[index];
if (item == "도로시")
items.RemoveAt(index);
}
Console.WriteLine(string.Join(", ", items));
Console.ReadLine();
}
}
소스 위치
github: https://github.com/opzerg/YGStudy.git
solution: 관용구
project RemoveItemAtForLoop