[C#]event/add/remove语法糖使用方法

2020/08 21 23:08
public class EventTest : MonoBehaviour
{
    public delegate void LogCallback();
    void Start()
    {
        LogCallback callback = ()=>
        {
            Debug.Log("Call");
        };

        MyLog += callback;
        MyLog -= callback;
        MyLog -= callback;
        MyLog += callback;

        if (LogStack != null)
        {
            LogStack.Invoke();
        }
    }

    private LogCallback LogStack;
    public event LogCallback MyLog
    {
        add
        {
            LogStack += value;
        }

        remove
        {
            LogStack -= value;
        }
    }
}