Delegates should be a local variable?

S

Sin Jeong-hun

I've been using delegates as class-wide variables, like:
class TheForm: Form
{
delegate void ChangeTextDelegate(string msg);
ChangeTextDelegate ctd;
public TheForm()
{
ctd=new ChangeTextDelegate(...)
}
void OnMessage(string msg)
{
if(InvokeRequired)
{
this.BeginInvoke(this.ctd,new object[]{msg});
}
else
{
TheTextBox.Text=msg;
}
}

But a few minutes ago I saw a sample code that used a delegate as a
local variable like:
class TheForm: Form
{
delegate void ChangeTextDelegate(string msg);
void OnMessage(string msg)
{
if(InvokeRequired)
{
ChangeTextDelegate ctd=new ChangeTextDelegate(...)
this.BeginInvoke(ctd,new object[]{msg});
}
else
{
TheTextBox.Text=msg;
}
}

Does this make any difference? If so, which is the correct way? Thank
you.
 
B

Barry Kelly

Sin said:
I've been using delegates as class-wide variables, like:
class TheForm: Form
{
delegate void ChangeTextDelegate(string msg);
ChangeTextDelegate ctd;

You should only create a field if you need to keep the value being
referred to around, IMO. Once initialized, this delegate will be kept
alive as long as the class (or until the field is overwritten).
But a few minutes ago I saw a sample code that used a delegate as a
local variable like:
ChangeTextDelegate ctd=new ChangeTextDelegate(...)
this.BeginInvoke(ctd,new object[]{msg});
Does this make any difference?

Sure, it minimizes the lifetime of the delegate instance, and it's more
encapsulated because the instance is located where it's used and not
elsewhere.

-- Barry
 
S

Sin Jeong-hun

Sin said:
I've been using delegates as class-wide variables, like:
class TheForm: Form
{
delegate void ChangeTextDelegate(string msg);
ChangeTextDelegate ctd;

You should only create a field if you need to keep the value being
referred to around, IMO. Once initialized, this delegate will be kept
alive as long as the class (or until the field is overwritten).
But a few minutes ago I saw a sample code that used a delegate as a
local variable like:
ChangeTextDelegate ctd=new ChangeTextDelegate(...)
this.BeginInvoke(ctd,new object[]{msg});
Does this make any difference?

Sure, it minimizes the lifetime of the delegate instance, and it's more
encapsulated because the instance is located where it's used and not
elsewhere.

-- Barry

--http://barrkel.blogspot.com/
Thank you.
That makes sense if OnMessage is rarely called. But what If OnMessage
is often
called from other threads, thus BeginInvoke is called often?
Should I still make the delegate as a local variable? I think creating
an instance of the
delegate each time could be an overhead, but I wonder if using a class
variable
delegate might cause some kind of side effect if OnMessage were called
from
many other threads at the same time.
 
J

Jon Skeet [C# MVP]

Sin Jeong-hun said:
That makes sense if OnMessage is rarely called. But what If OnMessage
is often called from other threads, thus BeginInvoke is called often?
Should I still make the delegate as a local variable? I think creating
an instance of the delegate each time could be an overhead, but I wonder
if using a class variable delegate might cause some kind of side effect
if OnMessage were called from many other threads at the same time.

It shouldn't be a problem to have multiple threads use the same
delegate at the same time, but it's confusing for the reader (at least,
it looked like a mistake rather than a deliberate decision to me, to
start with).

Until you have any evidence at all that creating the delegate each time
is a significant overhead, you shouldn't bend the design out of shape
for the sake of performance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top