C#

대리자

s0002 2023. 1. 11. 12:31

Delegate  클래스를 상속 받았음

대리자 인스턴스화: 대리자 인스턴스에 호출할 메서드 연결해서 대리자 변수에 할당

인스턴스가 메서드에 대한 참조를 갖고 있다

 

대리자 사용하는 법

1.연결할 메서드가 있음

2.대리자 정의

3.대리자 변수 정의

4.대리자 인스턴스화, 호출할 메서드에 연결, 변수에 할당

5.대리자 호출

using System;
using System.Collections.Generic;
using System.Text;

namespace Study10
{
    class App
    {
        //2. 대리자 정의: 클래스 안
        delegate int MyDelegate(int a, int b);

        //생성자
        public App()
        {
            //3. 변수 정의
            MyDelegate del;

            //4. 대리자 인스턴스화, 변수에 할당
            del=new MyDelegate(Plus);

            //5. 대리자 호출: 대리자에 연결된 메서드 호출
            int sum=del(1, 2);
            Console.WriteLine(sum);
        }


        //1. 메서드 생성
        int Plus (int a, int b)
        {
            return a+b;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace Study10
{
    class App
    {
        //2. 대리자 정의: 클래스 안
        delegate void MyDel(string contents);

        //생성자
        public App()
        {
            //3. 대리자 변수 정의
            MyDel myDel;

            //4. 대리자 인스턴스 생성, 메서드 연결
            myDel = new MyDel(this.Print);

            this.LoadFile(myDel);           
        }

      
        void  LoadFile(MyDel myDel)
        {
            //파일 읽기
            Console.WriteLine("파일 읽는중");
            //파일을 다 읽음 (이벤트) :LOAD_COMPLETE (상태)
 
            string contents = "hello world"; //파일에서 나온 문자열

            //5. 대리자 호출
            //null 체크 필요
            if (myDel == null)
            {
                //프린트 안하겠다
            }
            else
            {
                myDel(contents);
            }            
        }

        //1.대리자에 연결할 메서드
        void Print(string contents)
        {
            //출력
            Console.WriteLine(contents);
        }
    }
}

 

 

익명 메서드

1.어떤 메서드를 연결 할 건지 생각

2.대리자 형식 정의

3.대리자 변수 정의

4.대리자 인스턴스화, 메서드 연결

5.대리자 호출

using System;
using System.Collections.Generic;
using System.Text;

namespace Study10
{
    class App
    {
        //익명 메서드

        //2.대리자 형식 정의
        delegate int MyDel(int a, int b);

        //생성자
        public App()
        {        
            //1. 머리속으로 생각한다 - 두 정수를 더해서 반환하는 메서드야
            //3. 대리자 변수 정의
            MyDel del;

            //4. 대리자 인스턴스화(메서드 연결)
            del = delegate (int a, int b)
              {
                  return a + b;
              }; //; 빼먹지 말기

            //5. 대리자 호출
            int result=del(1, 2);
            Console.WriteLine(result);
        }
    }  
      
}

 

 

람다

using System;
using System.Collections.Generic;
using System.Text;

namespace Study10
{
    class App
    {
        //대리자 형식
        delegate int Mydel(int a, int b);
        //생성자
        public App()
        {
            //1.메서드: 두 수의 합을 반환하는 메서드

            //3.대리자 변수 정의
            Mydel del;

            //4.대리자 인스턴스화------------------
            del = delegate (int a, int b) //람다 x
              {
                  return a + b;
              };

            del = (int a, int b) => { 
                return a + b;
            }; //람다

            del = (a, b) =>
            {
                return a + b;
            }; //문 람다+형식유추

            del=(a,b) => a + b; //람다 식+형식유추
            //-------------------------------------

            //문 람다 {}

            //식 람다 본문이 반환값 포함 한 문장
        }
    }      
}

 

Action<T> :반환 값 x, 매개변수 타입T 대리자 형식

https://learn.microsoft.com/ko-kr/dotnet/api/system.action-1?view=net-7.0 

 

Action<T> 대리자 (System)

매개 변수가 하나이고 값을 반환하지 않는 메서드를 캡슐화합니다.

learn.microsoft.com

Func<T,TResult>:반환 값 o, 매개변수 타입T 대리자 형식

https://learn.microsoft.com/ko-kr/dotnet/api/system.func-2?view=net-7.0 

 

Func<T,TResult> 대리자 (System)

매개 변수가 하나이고 TResult 매개 변수에 지정된 형식의 값을 반환하는 메서드를 캡슐화합니다.

learn.microsoft.com