Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6822181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:37:58+00:00 2026-05-26T21:37:58+00:00

I’m wondering if I do something like this: class A { public MethodA() {

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:37:58+00:00Added an answer on May 26, 2026 at 9:37 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.