728x90
반응형
728x170
▶ IEnumerable<T> 인터페이스 : 변환 함수를 사용해 열거 가능형 변환하기 예제
using System;
using System.Collections.Generic;
IEnumerable<string> sourceEnumerable = new string[]
{
"광주",
"대구",
"대전",
"부산",
"서울"
};
IEnumerable<string> resultEnumerable = Convert<string, string>(sourceEnumerable, x => 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 static IEnumerable<TTarget> Convert<TSource, TTarget>
(
IEnumerable<TSource> sourceEnumerable,
Func<TSource, TTarget> convertFunction
)
{
foreach(TSource source in sourceEnumerable)
{
TTarget target = convertFunction(source);
yield return target;
}
}
#endregion
728x90
반응형
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON] peverify.exe 프로그램 (0) | 2019.11.28 |
---|---|
[C#/COMMON] sn.exe 프로그램 : 서명 파일 생성하기 (0) | 2019.11.28 |
[C#/COMMON] perfview.exe 프로그램 (0) | 2019.11.28 |
[C#/COMMON] Process 클래스 : ProcessAffinity 속성을 사용해 프로세스를 특정 CPU에서만 실행하기 (0) | 2019.11.25 |
[C#/COMMON] IEnumerable<T> 인터페이스 : 변환 함수를 사용해 열거 가능형 변환하기 (0) | 2019.11.24 |
[C#/COMMON] Lazy<T> 클래스 : 변환 함수를 사용해 지연 객체 변환하기 (0) | 2019.11.24 |
[C#/COMMON] Lazy<T> 클래스 : 변환 함수를 사용해 지연 객체 변환하기 (0) | 2019.11.24 |
[C#/COMMON] 변환 함수를 사용해 객체 변환하기 (0) | 2019.11.24 |
[C#/COMMON] 변환 함수를 사용해 NULL 가능형 구조체 값 변환하기 (0) | 2019.11.24 |
[C#/COMMON] 변환 함수를 사용해 NULL 가능형 구조체 값 변환하기 (0) | 2019.11.24 |
댓글을 달아 주세요