2016년 8월 23일 화요일

[C#강좌,닷넷강좌,IT실무교육★탑크리에듀][C#링크예제]메소드쿼리식에서 Aggregate사용간단예제

[C#링크예제]메소드쿼리식에서 Aggregate사용간단예제

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqAggregationExam
{
    class Program
    {
        static void Main(string[] args)
        {
            //Aggregate는 값을 포워드 하면서 주어진 연산을 수행한다.
            var numbers = new int[] { 1, 2, 3, 4, 5 };

            // 1*2 한 후 결과를 3과 곱하고, 다시 결과를 4와 곱함...
            var result = numbers.Aggregate((a, b) => a * b);
            Console.WriteLine(result);

            // 10은 SEED, 10+1 결과를 2와 더하고 다시 결과를 3과 더함...
            result = numbers.Aggregate(10, (a, b) => a + b);
            Console.WriteLine(result);
        }
    }
}

[결과]
120
25

댓글 없음:

댓글 쓰기