kubal5003
kubal5003

Reputation: 7254

C# events and class methods

I'm wondering if I do something like this:

class A
{
    public MethodA()
    {
    }
    public MethodB()
    {
        ExternalObject.Click += this.MethodA;
    }
    public MethodC()
    {
        ExternalObject.Click -= this.MethodA;
    }
}

A a = new A();
a.MethodB();
a.MethodC();

Is this going to work? By "work" I mean - will MethodA be unsubscribed from ExternalObject.Click event?

And other related questions:

What happens behind the scenes when an instance method is used instead of delegate instance? (like above)

Does this cause implicit creation of a delegate?

How does -= operator perform comparison between delegates - by reference or maybe something more sophisticated happens?

Upvotes: 6

Views: 120

Answers (2)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391336

Yes, it will work like you want it.

The comparison is not by reference, it is by value, which means that you can unsubscribe a different delegate, as long as it points to the same method on the same object.

In other words, this will work just fine:

var delegate1 = new ExternalObjectClickEventHandler(MethodA);
var delegate2 = new ExternalObjectClickEventHandler(MethodA);
ExternalObject.Click += delegate1;
ExternalObject.Click -= delegate2;

Anonymous methods are different though, you cannot do this:

public MethodB()
{
    ExternalObject.Click += () => { return 10; };
}

public MethodC()
{
    ExternalObject.Click -= () => { return 10; };
}

While the methods contain the same code, they are considered different, and thus this won't work, that is, MethodC will not unsubscribe the delegate you added in MethodB.

To solve this problem, you have to store the delegate between invocations, like this:

private ExternalObjectClickEventHandler _ClickEventHandler;
public MethodB()
{
    _ClickEventHandler = () => { return 10; };
    ExternalObject.Click += _ClickEventHandler;
}

public MethodC()
{
    ExternalObject.Click -= _ClickEventHandler;
}

But the code you showed, will work.

As for your question what happens behind the scenes, is that the following two lines of code are identical when it comes to generated code:

ExternalObject.Click += MethodA;
ExternalObject.Click += new ExternalObjectClickEventHandler(MethodA);

The second code is what is generated from the first (assuming the type of the event is as shown.)

The first syntax was (at some point) added as "syntactic sugar" that gets translated by the compiler as though you had written the second syntax. Note that this only happens if the compiled can figure out the right type 100%. I've seen some cases (which I can't remember right now) where you had to use the fully qualified syntax because the compiler couldn't figure out what you meant. In particular, method overloads and generics can confuse it in this regard.

Upvotes: 8

Christoph Fink
Christoph Fink

Reputation: 23103

Yes, MethodA will be unsubscribed.

About the second question I'm not 100% sure, but I think = this.MethodA converts to a delegate in the background.

Upvotes: 3

Related Questions