[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");
}
}
}
}
}
[위는 경력치 기준이 달라지면 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");
}
}
}
}
}
https://youtu.be/s0tkKZoMN1Y?list=PLAC325451207E3105
[C#] Delegate 이해[C#] Delegate 이해
- 이전글[C#] Linq Object Sample 18.07.25
- 다음글[C#] What is the difference between is and as keyword in c# 18.07.25
댓글목록
등록된 댓글이 없습니다.