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 734081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:19:51+00:00 2026-05-14T07:19:51+00:00

I am trying to learn on how to use delegates efficiently in C# and

  • 0

I am trying to learn on how to use delegates efficiently in C# and I was just wondering if anyone can guide me through… The following is a sample implementation using delegates… All I am doing is just passing a value through a delegate from one class to another… Please tell me if this is the right way to implement… And also your suggestions…

Also, please note that I have de-registered the delegate in :

void FrmSample_FormClosing(object sender, FormClosingEventArgs e)
{
     sampleObj.AssignValue -= new Sample.AssignValueDelegate(AssignValue);
}

Is this de-registration necessary?

The following is the code that I have written..

public partial class FrmSample : Form
{
    Sample sampleObj;

    public FrmSample()
    {
        InitializeComponent();

        this.Load += new EventHandler(FrmSample_Load);
        this.FormClosing += new FormClosingEventHandler(FrmSample_FormClosing);

        sampleObj = new Sample();
        sampleObj.AssignValue = new Sample.AssignValueDelegate(AssignValue);            
    }

    void FrmSample_FormClosing(object sender, FormClosingEventArgs e)
    {
        sampleObj.AssignValue -= new Sample.AssignValueDelegate(AssignValue);
    }

    void FrmSample_Load(object sender, EventArgs e)
    {
        sampleObj.LoadValue();
    }

    void AssignValue(string value)
    {
        MessageBox.Show(value);
    }
}

class Sample
{
    public delegate void AssignValueDelegate(string value);
    public AssignValueDelegate AssignValue;

    internal void LoadValue()
    {
        if (AssignValue != null)
        {
            AssignValue("This is a test message");
        }
    }
}

Pls provide your feedback on whether this is right…

Thanks,
Ram

  • 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-14T07:19:52+00:00Added an answer on May 14, 2026 at 7:19 am

    Explicitly unregistering event handlers is only necessary when the event reference keeps the object that contains the delegate target alive for too long. In your case, the sampleObj will have a reference to the Form object. The C# syntax sugar hides this, the compiler actually generates this code:

    sampleObj.AssignValue = new
    Sample.AssignValueDelegate(this,
    AssignValue);

    Were the this argument initializes the Delegate.Target property and the AssignValue argument initializes the Delegate.Method property.

    Which means that as long as the sampleObj object stays referenced, the form object stays referenced too and won’t be garbage collected. However, in your case, the only object that has a reference to sampleObj is the form object itself. The garbage collector has no trouble with circular references like this and will detect that there are no other references to either object. And will collect them both at the same time.

    There is one (uncommon) exception to this, you’ll get in trouble with the sampleObj class generates events after the form is closed. Which could happen if the event is triggered by something outside of the form, some hardware event for example. That event could still run code in the form class. You usually notice this quickly, any attempt to reference a control in the form will throw an ObjectDisposed exception.

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

Sidebar

Related Questions

I am trying to learn how to use MSBuild so we can use it
I'm trying to learn to use SDL for a little game I'm writing ,
I am trying to learn CodeIgniter to use for a shopping site, but I
I'm trying to learn advanced sql and how to use system queries (sql server).
I've just learnt a bit jQuery, and am trying to use it for a
I'm trying to learn how to use the UIPickerView, and I don't know why
I am trying to learn how to use threads with python. this is the
I am trying to learn and use an SDK for a vendor's product. Unfortunately,
I'm trying to learn to use vimdiff well. Currently I figured out how to
Trying to learn a bit of CSS and I want a horizontal navbar and

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.