w0051977
w0051977

Reputation: 15817

.NET - Event ADD and REMOVE

I have never used 'add' and 'remove' when creating and using events in .NET. Have a look at the code below:

public event EventHandler InitComplete
    {
        add
        {
            base.Events.AddHandler(EventInitComplete, value);
        }
        remove
        {
            base.Events.RemoveHandler(EventInitComplete, value);
        }
    }

I realise this is a very simple question, but where is 'value' declared? Can anyone recommend a good tutorial on use of add and remove. I have read through a few but am still not wise enough.

Upvotes: 1

Views: 986

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502006

value is "whatever handler is being subscribed or unsubscribed". So you can think of:

button.Click += HandleClick;

as being similar to:

button.add_Click(new EventHandler(HandleClick));

In that respect, an event is just like a property:

private string foo;
public string Foo
{
    get { return foo; }
    set { foo = value; }
}

... except that with events you have add and remove instead of get and set.

See my article on events and delegates for some more information - or section 10.8 of the C# 4 language specification for the details.

Upvotes: 7

Related Questions