DBILITY

독거 가능성 100% 노후에 라면값이라도 하게 센스를 발휘합시다!😅
Please click on the ad so that I can pay for ramen in my old age!
点击一下广告,让老后吃个泡面钱吧!
老後にラーメン代だけでもするように広告を一回クリックしてください。

c# delegate 본문

C#

c# delegate

DBILITY 2019. 4. 2. 17:32
반응형

대리자라고들 하고 번역대로라면 위임하다정도 되겠다.

뭘 위임한다는 것일까..결국 Method에 대한 참조다. callback method를 생각해 보자.

피호출자에게 호출자의 메서드를 넘겨주고 피호출자가 그 메서드를 실행한다.

결국 메모리상의 메서드(함수)의 위치값을 넘기는게 되는거지..뭐라는건지ㅎㅎ

C# 전문가가 아니다보니 그동안 전통적 방식의 델리게이트만 썼는데, 2.0부터 간소화되었군..

 

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            A Test = new A();

            DelegateType DelMethod1 = new DelegateType(Test.PrintA);
            DelMethod1("Hello Traditional C#1.0 Delegate usage");

            DelegateType DelMethod2 = Test.PrintA;
            DelMethod2("Good C#2.0 Delegate usage");

            DelMethod1 += Test.PrintB;
            DelMethod1("Hello Traditional C#1.0 Delegate usage");
            DelMethod1 -= Test.PrintA;
            DelMethod1("Hello Traditional C#1.0 Delegate usage");
        }
    }

    delegate void DelegateType(string str);
    class A
    {
        public void PrintA(string str)
        {
            Console.WriteLine(string.Format("PrintA {0}", str));
        }

        public void PrintB(string str)
        {
            Console.WriteLine(string.Format("PrintB {0}", str));
        }
    }
}

반응형

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

visual studio sdk  (0) 2020.10.11
c# Casing Convention  (0) 2019.04.02
C# Rect 구조체 사용시 WindowsBase.dll의 참조를 추가해야 한다.  (0) 2019.04.02
c# dllimport Platform Invoke시 signiture 참조  (0) 2019.04.02
C# R Client  (0) 2019.02.27
Comments