[C#] Delegate 이해 > C#/.Net/Blazor/IIS

본문 바로가기
사이트 내 전체검색

C#/.Net/Blazor/IIS

[C#] Delegate 이해

페이지 정보

작성자 sbLAB 댓글 0건 조회 6,470회 작성일 18-07-25 17:39

본문

[Delegate 를 선언해주고, 타켓 메서드를 참조하도록 연결 한다.]



using System;
 
namespace delegateEx
{
 
    public delegate void HelloFunctionDelegate(string Msg);
 
    class Program
    {
        static void Main(string[] args)
        {
            //A delegate is a "type safe" function pointer
            HelloFunctionDelegate del = new HelloFunctionDelegate(Hello);
            del("Hello from Delegate");
        }
 
        public static void Hello(string strMsg)
        {
            Console.WriteLine(strMsg);
        }
 
    }
}

[아래는 Employee 클래스에서 각 직원데이타를 입력 받아, 
Employee 클래스에 있는 조건문에 따라 경력에 따른 분류작업이 이뤄진다]


using System;
using System.Collections.Generic;
 
namespace delegateEx
{
 
    class Program
    {
        static void Main(string[] args)
        {
          List<Employee> empList = new List<Employee>();
 
          empList.Add(new Employee() { ID = 101, Name = "Mart", Salary = 5000, Experience = 5 });
          empList.Add(new Employee() { ID = 102, Name = "Mike", Salary = 4000, Experience = 4 });
          empList.Add(new Employee() { ID = 103, Name = "John", Salary = 6000, Experience = 6 });
          empList.Add(new Employee() { ID = 104, Name = "Todd", Salary = 3000, Experience = 3 });
 
          Employee.PromoteEmployee(empList);
 
        }
    }
 
 
    class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Salary { get; set; }
        public int Experience { get; set; }
 
        public static void PromoteEmployee(List<Employee> employeeList)
        {
            foreach (Employee employee in employeeList)
            {
                if (employee.Experience >= 5)
                {
                    Console.WriteLine(employee.Name + " promoted");
                }
 
            }
 
        }
 
    }
 
 
}

4cb3f0ba517befeb9b243ed5915443ee_1532508016_8702.jpg

[위는 경력치 기준이 달라지면 Employee 클래스에서 그 조건문을 다시 수정해야 한다. 
이를 피하려고 기준 경력수치 비교문을 Employee 클래스에서 빼내는데, 이때 delegate 를 사용하고 있다.]
※ Employee 클래스의 PromoteEmployee 메서드 파라미터에 경력치 조건판단 메서드를 넣고자 할때,
  이처럼 포인터 역할을 하는 델리게이트를 넣어주면서, 바깥에 있는 메서드를 실행시켜 주고 있는 것이다.


using System;
using System.Collections.Generic;
 
namespace delegateEx
{
    delegate bool IsPromotable(Employee empl);
 
    class Program
    {
        static void Main(string[] args)
        {
          //A delegate is a "type safe" function pointer
          List<Employee> empList = new List<Employee>();
          empList.Add(new Employee() { ID = 101, Name = "Mart", Salary = 5000, Experience = 5 });
          empList.Add(new Employee() { ID = 102, Name = "Mike", Salary = 4000, Experience = 4 });
          empList.Add(new Employee() { ID = 103, Name = "John", Salary = 6000, Experience = 6 });
          empList.Add(new Employee() { ID = 104, Name = "Todd", Salary = 3000, Experience = 3 });
 
          IsPromotable isPromotable = new IsPromotable(Promote);
          Employee.PromoteEmployee(empList, isPromotable);
 
        }
 
        //델리게이트에 사용될 체킹 메서드
        public static bool Promote(Employee emp)
        {
            if (emp.Experience >= 5)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
 
    class Employee
    {
    	public int ID { get; set; }
   	public string Name { get; set; }
   	public int Salary { get; set; }
    	public int Experience { get; set; }

public static void PromoteEmployee(List<Employee> employeeList, IsPromotable IsEligibleToPromote)
        {
            foreach (Employee employee in employeeList)
            {
                if (IsEligibleToPromote(employee))
                {
                    Console.WriteLine(employee.Name + " promoted");
                }
 
            }
 
        }
 
    }
}

4cb3f0ba517befeb9b243ed5915443ee_1532508052_869.jpg


https://youtu.be/s0tkKZoMN1Y?list=PLAC325451207E3105

 



[C#] Delegate 이해[C#] Delegate 이해

댓글목록

등록된 댓글이 없습니다.

회원로그인

접속자집계

오늘
237
어제
417
최대
1,279
전체
221,008

그누보드5
Copyright © sebom.com All rights reserved.