길이가 14인 리스트가 있다. 길이에 상관없이 리스트에서 5개씩 꺼내서 작업할 때 쓰는 표현이다. 확장메서드와 제네릭 타입을 이용해 꽤 괜찮게 표현할 수 있다.
public static IEnumerable<List<T>> SplitList<T>(this List<T> list, int count)
{
for (int index = 0; index < list.Count; index += count)
{
yield return list.GetRange(index, Math.Min(count, list.Count - index));
}
}
소스 위치
github: https://github.com/opzerg/YGStudy.git
solution: 관용구
project Extension