갓똥
나는야 프로그래머
갓똥
전체 방문자
오늘
어제
  • 분류 전체보기 (186)
    • 프로그래밍 (146)
      • 자바 (9)
      • 안드로이드 (2)
      • 유니티 (20)
      • C++ (38)
      • C# (56)
      • HTML (2)
      • 파이썬 (3)
      • 자료구조 (2)
      • 알고리즘 (0)
      • 문제풀이 (4)
      • 디자인 패턴 (7)
      • 카카오톡 봇 (1)
      • 엑셀 (1)
      • 기타 (1)
    • 게임 (21)
      • 테일즈위버 (0)
      • 카이로소프트 (1)
      • 순위 (19)
      • 기타 (1)
    • 일상 (13)
      • 카페 (1)
      • 방탈출 (12)
    • 기타 (6)
      • 웃긴자료 (5)

블로그 메뉴

  • 홈
  • 방명록

공지사항

인기 글

태그

  • C++ 소멸자
  • 유니티 그래프 그리기
  • pc게임 순위
  • c# coroutine
  • pc 게임 순위
  • 유니티 골드그래프
  • C++ virtual
  • 게임 매출 순위
  • Unity Graph
  • 게임 디자인 패턴
  • 유니티 그래프
  • 2020년 게임 매출
  • 게임매출순위
  • c# unboxing
  • c# 코루틴
  • C# 예외 처리
  • c# Thread
  • 글로벌게임매출
  • c# delegate
  • 강남 방탈출
  • C# boxing
  • 자바
  • 모바일 게임 순위
  • 알고리즘
  • 전세계 게임 매출
  • 롤 골드그래프
  • c# collection
  • C++ 상속
  • 전세계게임매출순위
  • C++

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
갓똥
프로그래밍/C#

[C#] Delegate Chain

[C#] Delegate Chain
프로그래밍/C#

[C#] Delegate Chain

2020. 4. 7. 16:44
728x90
반응형

1. Delegate Combine

using System;

class Test {
    public static int Method1() {
        Console.WriteLine("Method1");
        return 1;
    }
    
    public static int Method2() {
        Console.WriteLine("Method2");
        return 2;
    }
    
    public static int Method3() {
        Console.WriteLine("Method3");
        return 3;
    }
    
    public static int Method4() {
        Console.WriteLine("Method4");
        return 4;
    }
}

delegate int FUNC();

class Program {
    public static void Main() {
        FUNC f1 = Test.Method1;
        FUNC f2 = Test.Method2;
        FUNC f3 = Test.Method3;
        
        // 1. Delegate Combine
        FUNC f4 = (FUNC)Delegate.Combine(f1, f2);     // - ok
        // FUNC f4 = (FUNC)Delegate.Combine(f1, f2, f3); - ok
        // FUNC f4 = (FUNC)Delegate.Combine(f1, Test.Method4);  - error
        f4();  // Method1, Method2 호출
        
        // 2. +, -, +=, -=
        FUNC f5 = f1 + f2;     //  - ok
        // FUNC f5 = f1 + f2 + f3; - ok
        // FUNC f5 = f1 + Test.Method4 + f2; - ok
        f5();  // Method1, Method2 호출
    }
}

 ① Delegate.Combine(delegate1, delegate2, ...)

    => 기존의 delegate를 결합한 새로운 delegate 객체 반환

    => Method 이름을 직접 사용할 수는 없음

 

 ② +, +=, -, -= 연산자

    => Delegate 이름 또는 Method 이름을 사용한 결합

 

 ③ Delegate는 immutable 하다.

    => 기존의 delegate 객체에 추가되는 것이 아닌 새로운 delegate 객체를 생성하는 것

delegate는 reference type이다.
FUNC f6 = Test.Method1; 으로 객체를 만들고
FUNC f7 = f6; 으로 복사한다면
둘은 Heap에 같은 곳을 보게되므로 == 연산자를 통해 비교하면 true가 나온다.
여기서
f6 += Test.Method2; 를 하게 된다면
f6 = f6 + Test.Method2; 와 같은 코드가 아니라
f6 = new FUNC( ... ) 와 같은 코드가 되어서 f7은 변하지 않는다.
그리고 둘은 다른 객체가 된다 (==연산자 시 false)

 


2. Delegate Chain 리턴 값

using System;

class Test {
    public static int Method1() {
        Console.WriteLine("Method1");
        return 1;
    }
    
    public static int Method2() {
        Console.WriteLine("Method2");
        return 2;
    }
    
    public static int Method3() {
        Console.WriteLine("Method3");
        return 3;
    }
    
    public static int Method4() {
        Console.WriteLine("Method4");
        return 4;
    }
}

delegate int FUNC();

class Program {
    public static void Main() {
        FUNC uni = Test.Method1;
        
        int ret1 = uni();
        Console.WriteLine(ret1); // 1
        
        
        FUNC multi = Test.Method1;
        multi += Test.Method2;
        multi += Test.Method3;
        multi += Test.Method4;
        
        int ret2 = multi();
        Console.WriteLine(ret2); // 4
    }
}

 

실행 결과

Method1
1
Method1
Method2
Method3
Method4
4
multi는 메소드4개를 가지고 있지만 반환값은 마지막 메소드 하나이다.
하다보면 모든 메소드의 반환값을 받고 싶을 때도 있을텐데
GetInvocationList() 라는 메소드를 사용해서 얻을 수 있다.
메소드를 사용하면 모든 반환값이 배열형태로 반환된다.
Delegate타입의 배열을 만들어 받고
foreach문을 통해 하나씩 돌려주면 된다.
using System;

class Test {
    public static int Method1() {
        Console.WriteLine("Method1");
        return 1;
    }
    
    public static int Method2() {
        Console.WriteLine("Method2");
        return 2;
    }
    
    public static int Method3() {
        Console.WriteLine("Method3");
        return 3;
    }
    
    public static int Method4() {
        Console.WriteLine("Method4");
        return 4;
    }
}

delegate int FUNC();

class Program {
    public static void Main() {
        FUNC multi = Test.Method1;
        multi += Test.Method2;
        multi += Test.Method3;
        multi += Test.Method4;
        
        Delegate[] arr = multi.GetInvocationList();
        
        foreach(Delegate d in arr) {
            FUNC f = (FUNC)d;
            int ret = f();
            Console.WriteLine(ret);
        }
    }
}

 

실행 결과

Method1
1
Method2
2
Method3
3
Method4
4

 ① Multicast Delegate의 반환값 얻기

    => GetInvocationList() 사용

728x90
반응형

'프로그래밍 > C#' 카테고리의 다른 글

[C#] Action, Func  (0) 2020.04.16
[C#] event  (0) 2020.04.08
[C#] Delegate Example  (0) 2020.04.06
[C#] Delegate (2)  (0) 2020.04.04
[C#] Delegate (1)  (0) 2020.03.30
  • 1. Delegate Combine
  • 2. Delegate Chain 리턴 값
'프로그래밍/C#' 카테고리의 다른 글
  • [C#] Action, Func
  • [C#] event
  • [C#] Delegate Example
  • [C#] Delegate (2)
갓똥
갓똥
공부하며 알아가는 내용을 정리해 봅니다.

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.