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

  • Home
  • SEARCH
  • 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 427041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:32:24+00:00 2026-05-12T19:32:24+00:00

I thought that a delegate instance was interchangeable with a function instance. Take the

  • 0

I thought that a delegate instance was interchangeable with a function instance.

Take the following code:

delegate int AddDelegate(int a, int b);

AddDelegate DelegateInstance;

public void DoStuff()
{
    //I can call this without a delegate "instance":
    MethodThatTakesAdd(Add);

    //I can also call it WITH a delegate "instance"
    DelegateInstance = Add;
    MethodThatTakesAdd(DelegateInstance);
}

public int Add(int a, int b)
{
    return a + b;
}

public void MethodThatTakesAdd(AddDelegate addFunction)
{
    Console.WriteLine(addFunction(1, 2).ToString());
}

Both ways of calling it APPEAR to be equivalent, and if you’re using only C#, you’ll never see the difference (at least I have not up to this point). However, I was recently unmanaged code that was calling back into this managed code, they are treated differently. For example, in one scenario, I to get the error “A callback was made on a garbage collected delegate” if I use the function directly as a callback (even though my object instance is kept around). Using the “delegate instance” fixes the problem.

Is there someone out there that knows what the difference is?

  • 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-12T19:32:24+00:00Added an answer on May 12, 2026 at 7:32 pm

    Terminology Corretion: Instead of method pointer, the more appropriate term is method group.

    In terms of functionality the two statements are equivalent. That is that they produce almost the same IL. The difference is where the delegate value is stored.

    In the first case you pass the method group Add to MethodThatTakesAdd directly. This causes a temporary delegate value to be created and then passed to MethodThatTakesAdd. This delegate value is subject to garbage collection the moment the MethodThatTakesAdd returns since it does not store the value.

    In the second case you assigned the delegate to a field on the outer instance. This will typically increase the lifetime of the delegate and hence reduce the chance it’s garbage collected during your pinvoke call.

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

Sidebar

Related Questions

I thought that calling BeginInvoke more than once on the same delegate instance would
I thought that I improve performance when I replace this code: def f(a, b):
I've always thought that it's impossible for this to be null inside instance method
I thought that by convention, only methods with an exclamation mark altered the object.
I thought that a 32-bit process could use at most 2GB RAM, since half
I thought that by default my Regex would exhibit the greedy behavior that I
I thought that this was easier… I have a asp:hyperlink control, with target=_blank ,
I thought that layout is just a widget that keeps more widgets inside. But
I thought that in principle Haskell's type system would forbid calls to impure functions
I thought that the purpose of Ruby's BigDecimal class is that it is infinitely

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.