블로그 이미지
따시쿵

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

2015. 6. 11. 16:15 C#

 

3. LINQ를 이용한 방법으로 표현하는 경우

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

namespace MylambdaExpression11
{
    class Program
    {
        static void Main(string[] args)
        {
            // 예제 - 1 
            // 다음 예제에서는 Func<T, TResult> 델리게이트를 선언하고 사용하는 방법을 보여 줍니다. 
            // 이 예제에서는 Func<T, TResult> 변수를 선언하고 문자열의 문자를 대문자로 변환하는 람다 식을 할당합니다. 
            // 이 메서드를 캡슐화하는 대리자는 이후에 문자열 배열의 문자열을 대문자로 변환하기 
            // 위해 Enumerable.Select 메서드에 전달됩니다. 

            Func<string, string> selector = (string s) => s.ToUpper();

            string[] words = {"orange", "apple", "Article", "elephant", "korea", "komani" };

            IEnumerable<string> aWords = words.Select(selector);

            foreach(string word in aWords)
                Console.WriteLine(word);

            Console.WriteLine();

            // 에제 - 2
            // words 배열에서 소문자 "k"로 시작하는 단어를 찾아서 알려줍니다.
            int count = words.Count(s => s.StartsWith("k"));
            Console.WriteLine("count = {0}", count);

            Console.ReadLine();
        }
    }
}

 

실행화면 입니다.

 

'C#' 카테고리의 다른 글

람다식(Lambda Expression) - 3  (0) 2015.06.09
람다식(Lambda Expression) - 2  (0) 2015.06.09
람다식(Lambda Expression) - 1  (0) 2015.06.08
EventHandler 와 EventHandler<TEventArgs> Delegate  (0) 2015.06.04
Event 와 Delegate  (0) 2015.06.01
posted by 따시쿵