[C# 델리게이트 예제]델리게이트를 메소드의 인자로...
					
						꽁스짱					
																
							
							
							C#						
										
					
					0					
					
					3606
															
						
						
							2021.02.15 23:02						
					
				
			[C# 델리게이트 예제]델리게이트를 메소드의 인자로...
using System;
using System.Collections.Generic;
using System.Linq;
namespace DelegateApp
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    class Program
    {
        //Our delegate
        public delegate bool FilterDelegate(Person p);
        static void Main(string[] args)
        {
            Person p1 = new Person() { Name = "이종철", Age = 41 };
            Person p2 = new Person() { Name = "박용진", Age = 68 };
            Person p3 = new Person() { Name = "오라클자바커뮤니티", Age = 18 };
            Person p4 = new Person() { Name = "김성박", Age = 20 };
            List<Person> people = new List<Person>() { p1, p2, p3, p4 };
            //메소드를 호출할 때 델리게이트에 델리게이트가 참조할 메소드 이름을 인자로..
            DisplayPeople("미성년자:", people, Program.IsChild);
            DisplayPeople("성인:", people, Program.IsAdult);
            DisplayPeople("50넘은사람:", people, Program.IsSenior);
            Console.Read();
        }
        static void DisplayPeople(string title, List<Person> people, FilterDelegate filter)
        {
            Console.WriteLine(title);
            foreach (Person p in people)
            {
                if (filter(p))
                {
                    Console.WriteLine("{0}, {1} years old", p.Name, p.Age);
                }
            }
            Console.Write("\n\n");
        }
        static bool IsChild(Person p)
        {
            return p.Age < 18;
        }
        static bool IsAdult(Person p)
        {
            return p.Age >= 18;
        }
        static bool IsSenior(Person p)
        {
            return p.Age >= 50;
        }
    }
}
[결과]
미성년자:
오라클자바커뮤니티, 18 years old
성인:
이종철, 41 years old
박용진, 68 years old
오라클자바커뮤니티, 18 years old
김성박, 20 years old
50넘은사람:
박용진, 68 years old

 
															
