첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

▶ IEnumerable<T> 인터페이스 : 변환 함수를 사용해 열거 가능형 변환하기 예제

using System;
using System.Collections.Generic;

IEnumerable<string> sourceEnumerable = new string[]
{
    "광주",
    "대구",
    "대전",
    "부산",
    "서울"
};

IEnumerable<string> resultEnumerable = Convert<string, string>
(
    sourceEnumerable,
    x => { return new string[] { x + "시" }; }
);

foreach(string result in resultEnumerable)
{
    Console.WriteLine(result);
}

 

728x90

 

▶ IEnumerable<T> 인터페이스 : 변환 함수를 사용해 열거 가능형 변환하기

using System;
using System.Collections.Generic;

#region 변환하기 - Convert<TSource, TTarget>(sourceEnumerable, convertFunction)

/// <summary>
/// 변환하기
/// </summary>
/// <typeparam name="TSource">소스 타입</typeparam>
/// <typeparam name="TTarget">타겟 타입</typeparam>
/// <param name="sourceEnumerable">소스 열거 가능형</param>
/// <param name="convertFunction">변환 함수</param>
/// <returns>타겟 열거 가능형</returns>
public IEnumerable<TTarget> Convert<TSource, TTarget>
(
    IEnumerable<TSource>                sourceEnumerable,
    Func<TSource, IEnumerable<TTarget>> convertFunction
)
{
    foreach(TSource source in sourceEnumerable)
    {
        IEnumerable<TTarget> targetEnumerable = convertFunction(source);

        foreach(TTarget target in targetEnumerable)
        {
            yield return target;
        }
    }
}

#endregion
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요