"Anonymous methods" and "closures"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter

    "Anonymous methods" and "closures"

    Hi

    I've been delving into "delegates" and "anonymous methods", and now
    I've come across the term "closure".

    Some information I've found says that C# does not have closures, other
    information says that C# does have closures.

    My problem is I can't quite grasp what "closures" are. The examples I
    have seen seem to be "anonymous methods". What is the subtle difference
    between a closure and an anonymous method?

    Thanks,
    Peter
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: "Anonym ous methods" and "closures& quot;

    Peter,

    Anonymous methods are closures. It's the fact that they can access
    variables outside it's scope that makes it a closure:

    http://en.wikipedia.org/wiki/Closure_(computer_science)


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Peter" <xdzgor@hotmail .comwrote in message
    news:ufufc33QJH A.444@TK2MSFTNG P05.phx.gbl...
    Hi
    >
    I've been delving into "delegates" and "anonymous methods", and now
    I've come across the term "closure".
    >
    Some information I've found says that C# does not have closures, other
    information says that C# does have closures.
    >
    My problem is I can't quite grasp what "closures" are. The examples I
    have seen seem to be "anonymous methods". What is the subtle difference
    between a closure and an anonymous method?
    >
    Thanks,
    Peter

    Comment

    • Peter

      #3
      Re: &quot;Anonym ous methods&quot; and &quot;closures& quot;

      Nicholas Paldino [.NET/C# MVP] wrote:
      Anonymous methods are closures. It's the fact that they can access
      variables outside it's scope that makes it a closure:
      >
      http://en.wikipedia.org/wiki/Closure_(computer_science)
      Thanks for the link. I've read through it once, but I'll need to read
      it a few times.

      Are you saying that all anonymous methods in C# are also closures - or
      just that anonymous methods which access outside variables can be
      termed closures? And anonymous methods which don't access outside
      variables are not closures (simply "anonymous methods").

      If "anonymous method" is just a synonym for "closure" then fine - but
      it sure confuses things when you're trying to get to grips with these
      concepts.

      Thanks,
      Peter

      Comment

      • Peter Duniho

        #4
        Re: &quot;Anonym ous methods&quot; and &quot;closures& quot;

        On Tue, 11 Nov 2008 00:38:45 -0800, Peter <xdzgor@hotmail .comwrote:
        [...]
        If "anonymous method" is just a synonym for "closure" then fine - but
        it sure confuses things when you're trying to get to grips with these
        concepts.
        "Closure" is a language feature. An anonymous method is a way to
        implement that feature, as are lambda expressions. "Anonymous method"
        definitely is not a synonym for "closure".

        At least based on the Wikipedia definition, an anonymous method wouldn't
        be a closure unless it actually did capture a variable. But for me
        personally, that distinction seems kind of silly. It's like saying that a
        named method without any literal references to external variables isn't
        actually a method, or a for() or while() loop that only ever executes once
        isn't actually a loop.

        To me, an anonymous method that doesn't capture outer variables may be a
        degenerate closure, but it's still a closure. :) YMMV. And even with
        that interpretation, that doesn't make the phrase "anonymous method"
        synonymous with "closure".

        Pete

        Comment

        • Peter Morris

          #5
          Re: &quot;Anonym ous methods&quot; and &quot;closures& quot;

          Think of it this way. Might not be right, but it works for me :-)

          public delegate void MyDelegate();

          public class MyClass
          {
          public void TwoExamples()
          {
          //Delegate to a method
          MyDelegate x = DoSomething;

          //Anonymous method enclosed in another method
          //A closure.
          MyDelegate y = delegate()
          {
          //A closure can access local variables
          //of it's containing method
          x = null;
          };
          }

          public void DoSomething()
          {
          //Here we have no concept of the local variable "x"
          }

          }



          --
          Pete
          ====


          Comment

          Working...