Delegates should be a local variable?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sin Jeong-hun

    #1

    Delegates should be a local variable?

    I've been using delegates as class-wide variables, like:
    class TheForm: Form
    {
    delegate void ChangeTextDeleg ate(string msg);
    ChangeTextDeleg ate ctd;
    public TheForm()
    {
    ctd=new ChangeTextDeleg ate(...)
    }
    void OnMessage(strin g msg)
    {
    if(InvokeRequir ed)
    {
    this.BeginInvok e(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 ChangeTextDeleg ate(string msg);
    void OnMessage(strin g msg)
    {
    if(InvokeRequir ed)
    {
    ChangeTextDeleg ate ctd=new ChangeTextDeleg ate(...)
    this.BeginInvok e(ctd,new object[]{msg});
    }
    else
    {
    TheTextBox.Text =msg;
    }
    }

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

  • Barry Kelly

    #2
    Re: Delegates should be a local variable?

    Sin Jeong-hun wrote:
    I've been using delegates as class-wide variables, like:
    class TheForm: Form
    {
    delegate void ChangeTextDeleg ate(string msg);
    ChangeTextDeleg ate 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:
    ChangeTextDeleg ate ctd=new ChangeTextDeleg ate(...)
    this.BeginInvok e(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

    --

    Comment

    • Sin Jeong-hun

      #3
      Re: Delegates should be a local variable?

      On Feb 26, 3:06 am, Barry Kelly <barry.j.ke...@ gmail.comwrote:
      Sin Jeong-hun wrote:
      I've been using delegates as class-wide variables, like:
      class TheForm: Form
      {
      delegate void ChangeTextDeleg ate(string msg);
      ChangeTextDeleg ate 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:
      ChangeTextDeleg ate ctd=new ChangeTextDeleg ate(...)
      this.BeginInvok e(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.blogspo t.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.

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Delegates should be a local variable?

        Sin Jeong-hun <typingcat@gmai l.comwrote:
        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.

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        Working...