[C#/LINQ/.NET5] AsyncEnumerable 클래스 : Where 확장 메소드를 사용해 동기 조건자 기반 비동기 열거 가능 시퀀스 필터링하기
C#/Linq 2021. 8. 15. 01:08728x90
반응형
728x170
▶ Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace TestProject
{
/// <summary>
/// 프로그램
/// </summary>
class Program
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
#region 값 열거 가능형 구하기 (비동기) - GetValueEnumerableAsync()
/// <summary>
/// 값 열거 가능형 구하기 (비동기)
/// </summary>
/// <returns>값 열거 가능형</returns>
private static async IAsyncEnumerable<int> GetValueEnumerableAsync()
{
for(int i = 0; i < 10; i++)
{
await Task.Delay(i * 100);
yield return i;
}
}
#endregion
#region 프로그램 시작하기 - Main()
/// <summary>
/// 프로그램 시작하기
/// </summary>
/// <returns>태스크</returns>
private static async Task Main()
{
IAsyncEnumerable<int> valueEnumerable = GetValueEnumerableAsync().Where(value => value % 2 == 0);
await foreach(int value in valueEnumerable)
{
Console.WriteLine(value);
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
댓글을 달아 주세요