Search code examples
c#game-engine

Should I be concerned over an event being added more than once?


I have a class instance which contains an event type. This event will be called if value is changed. I also contain a seperate function in Menu which must be called every time the program performs an action (changes menu screens). Should I be concerned over multiple onChange events occuring (or will multple onChange additions even occur)?

public class ValueHolder<T>
{
    public virtual T Value { /*get set stuff*/ }

    public event Action<T> onChange;
    // other necessary code
}

public class Menu
{
    ValueHolder<int> value = new ValueHolder<int>();

    void MenuChanged()
    {
        value.onChange += (i) =>
        {
            // do stuff
        };
    }
}

Solution

  • Each time your MenuChanged method will be called will add new anonymous method to the event invocation list, so yes, all of them would be called then.