Ram
Ram

Reputation: 1117

Implementation of delegates in C#

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

Upvotes: 2

Views: 2921

Answers (3)

Hans Passant
Hans Passant

Reputation: 941465

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.

Upvotes: 6

Phil Gan
Phil Gan

Reputation: 2863

An Action delegate may be more appropriate in this case - it's a specific form of delegate that takes 1 - 3 parameters with no return value:

Uses of Action delegate in C#

Upvotes: 1

Gopher
Gopher

Reputation: 927

It's not necessary to deregister delegates. You Sample obj lives in form scope and will be destroyed after form closing

Upvotes: 1

Related Questions