[C#] Delegate 멀티캐스트

c# 2023. 1. 14. 23:35
728x90
public partial class Program
{
    private delegate void CalculatorDelegate(int a, int b);

    private static void Add(int x, int y)
    {
        Console.WriteLine($"{x} + {y} = {x + y}");
    }

    private static void Substract(int x, int y)
    {
        Console.WriteLine($"{x} - {y} = {x - y}");
    }

    private static void Multiply(int x, int y)
    {
        Console.WriteLine($"{x} * {y} = {x * y}");
    }

    private static void Division(int x, int y)
    {
        Console.WriteLine($"{x} / {y} = {x / y}");
    }
}

public partial class Program
{
    private static void CombineDelegate()
    {
        CalculatorDelegate calcMultiples = (CalculatorDelegate)Delegate.Combine(new CalculatorDelegate[]
        {
            Add, Substract, Multiply, Division
        });
        Delegate[] calcList = calcMultiples.GetInvocationList();
        Console.WriteLine("Total delegates in calcMultiples: {0}", calcList.Length);

        calcMultiples(6, 3);
    }

    private static void RemoveDelegate()
    {
        CalculatorDelegate addDel = Add;
        CalculatorDelegate subDel = Substract;
        CalculatorDelegate mulDel = Multiply;
        CalculatorDelegate divDel = Division;

        CalculatorDelegate calcDelegates1 = (CalculatorDelegate)Delegate.Combine(addDel, subDel);
        CalculatorDelegate calcDelegates2 = (CalculatorDelegate)Delegate.Combine(calcDelegates1, mulDel);
        CalculatorDelegate calcDelegates3 = (CalculatorDelegate)Delegate.Combine(calcDelegates2, divDel);

        Console.WriteLine("Total delegates in calcDelegates3 : {0}", calcDelegates3.GetInvocationList().Length);
        calcDelegates3(6, 3);

        CalculatorDelegate calcDelegates4 = (CalculatorDelegate)Delegate.Remove(calcDelegates3, mulDel);

        Console.WriteLine("Total delegates in calcDelegates4 = {0}", calcDelegates4.GetInvocationList().Length);
        calcDelegates4(6, 3);
    }

    private static void DuplicateEntries()
    {
        CalculatorDelegate addDel = Add;
        CalculatorDelegate subDel = Substract;
        CalculatorDelegate mulDel = Multiply;

        CalculatorDelegate calcDelegates1 = (CalculatorDelegate)Delegate.Combine(addDel, subDel);
        CalculatorDelegate calcDelegates2 = (CalculatorDelegate)Delegate.Combine(calcDelegates1, mulDel);
        CalculatorDelegate calcDelegates3 = (CalculatorDelegate)Delegate.Combine(calcDelegates2, subDel);
        CalculatorDelegate calcDelegates4 = (CalculatorDelegate)Delegate.Remove(calcDelegates3, addDel);

        Console.WriteLine("Total delegates in calcDelegates4 = {0}", calcDelegates4.GetInvocationList().Length);
        calcDelegates4(6, 3);
    }
}
public partial class Program
{
    static void Main(string[] args)
    {
        // delegate 결합
        CombineDelegate();

        // delegate 제거
        RemoveDelegate();

        // 중복 메서드 포함
        DuplicateEntries();
    }
}

 

+=, +, -=, - 연산자 이용

CalculatorDelegate multiDel = addDel + subDel;
multiDel += mulDel;
multiDel += divDel;
multiDel(8, 2);

multiDel = multiDel - subDel;
multiDel -= mulDel;
multiDel(8, 2);
728x90

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

[C#] Delegate Action_Func  (0) 2023.01.15
[C#] Delegate 제네릭  (0) 2023.01.15
[C#] Delegate3  (0) 2023.01.14
[C#] Delegate2  (0) 2023.01.14
[C#] 메서드 체인  (0) 2023.01.13
Posted by 바르마스
,