Kontorted
Kontorted

Reputation: 226

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
        };
    }
}

Upvotes: 1

Views: 59

Answers (1)

Eduard Abliazimov
Eduard Abliazimov

Reputation: 261

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.

Upvotes: 3

Related Questions