첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

▶ FunctionalExtension.cs

using System;
using System.Threading.Tasks;

namespace TestProject
{
    /// <summary>
    /// 함수형 확장
    /// </summary>
    public static class FunctionalExtension
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 함수 실행하기 - ExecuteFunction<TSource, TResult>(sourceTask, function)

        /// <summary>
        /// 함수 실행하기
        /// </summary>
        /// <typeparam name="TSource">소스 타입</typeparam>
        /// <typeparam name="TResult">결과 타입</typeparam>
        /// <param name="sourceTask">소스 태스크</param>
        /// <param name="function">함수</param>
        /// <returns>태스크</returns>
        public static async Task<TResult> ExecuteFunction<TSource, TResult>
        (
            this Task<TSource>           sourceTask,
            Func<TSource, Task<TResult>> function
        )
        => await function(await sourceTask);

        #endregion
    }
}

 

728x90

 

▶ Program.cs

using System;
using System.Threading.Tasks;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    public class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region A 곱하기 (비동기) - MultiplyAAsync(a)

        /// <summary>
        /// A 곱하기 (비동기)
        /// </summary>
        /// <param name="a">A</param>
        /// <returns>곱셈 결과</returns>
        private async static Task<int> MultiplyAAsync(int a) => await Task.FromResult(a * 1);

        #endregion
        #region B 곱하기 (비동기) - MultiplyBAsync(b)

        /// <summary>
        /// B 곱하기 (비동기)
        /// </summary>
        /// <param name="b">B</param>
        /// <returns>곱셈 결과</returns>
        private async static Task<int> MultiplyBAsync(int b) => await Task.FromResult(b * 2);

        #endregion
        #region C 곱하기 (비동기) - MultiplyCAsync(c)

        /// <summary>
        /// C 곱하기 (비동기)
        /// </summary>
        /// <param name="c">C</param>
        /// <returns>곱셈 결과</returns>
        private async static Task<int> MultiplyCAsync(int c) => await Task.FromResult(c * 3);

        #endregion
        #region 곱하기 - Multiply()

        /// <summary>
        /// 곱하기
        /// </summary>
        public async static void Multiply()
        {
            int result = await MultiplyAAsync(10)
                .ExecuteFunction(MultiplyBAsync)
                .ExecuteFunction(MultiplyCAsync);

            Console.WriteLine("결과 : {0}", result);
        }

        #endregion
        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            Multiply();
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요