Unity/이론

[Unity] 7) Action<T>, Func<T,T>

GiveZero 2023. 3. 3. 11:02

Delegate 는 이전에 쓴 글과 같이

대리자로, 특정 매개변수 목록 및 반환 형식이 있는 메서드를 참조를 나타내는 형식으로,

메서드를 다른 메서드에 인수로 전달하는데 사용

 

 

Action<T>Func<T,TResult>

 모든 타입의 형태로 미리 정의된 delegate를 간단하게 쓸 수 있도록 하는 기능

// Action<T>
{
    public Action<string> testAction;

    testAction += TestA;
    testAction += Testb;

    testAction?.Invoke("Hello");

    void TestA(string s)
    {
        Debug.Log(s + "A");
    }

    void TestB(string s)
    {
        Debug.Log(s + "B");
    }
}


//Func<T,T>
{
    public Func<string,string> testFunc;

    testFunc = (str) => {return str;};
    Debug.Log(testFunc("HelloFunc"));
}