473,320 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

"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
Nov 10 '08 #1
4 2719
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]
- mv*@spam.guard.caspershouse.com

"Peter" <xd****@hotmail.comwrote in message
news:uf*************@TK2MSFTNGP05.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

Nov 10 '08 #2
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
Nov 11 '08 #3
On Tue, 11 Nov 2008 00:38:45 -0800, Peter <xd****@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
Nov 11 '08 #4
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
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com
Nov 11 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
11
by: Matt Kruse | last post by:
This is a common requirement - "freeze panes" in a table, so that some header rows and some columns on the left are frozen while the body content scrolls. This makes large tables more usable on...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
9
by: Christopher Benson-Manica | last post by:
In your experience, what is the prevalence of "good" C++ (i.e., the code makes full use of solid language features and the STL) versus C++ written in ignorance (or defiance) of one or more key...
3
by: rickeringill | last post by:
Hi comp.lang.javascript, I'm throwing this in for discussion. First up I don't claim to be any sort of authority on the ecmascript language spec - in fact I'm a relative newb to these more...
4
by: My Pet Programmer | last post by:
Ok guys, I'm really looking for someone to tell me how bad a hack this is, and if I'm close to where I should be with it. The basic situation is that I have a class which creates a basic...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.