Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# events and class methods

Tags:

c#

delegates

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?

like image 326
kubal5003 Avatar asked Nov 15 '11 12:11

kubal5003


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


1 Answers

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.

like image 196
Lasse V. Karlsen Avatar answered Sep 30 '22 20:09

Lasse V. Karlsen