리스트 자르기(SplitList)

최용국·2020년 4월 13일
0

관용구

목록 보기
6/9

리스트 자르기(SplitList)

길이가 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

profile
코딩합시다.

0개의 댓글