Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Comparison of C Sharp and Java
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Closures === A closure is an inline function that captures variables from its lexical scope. C# supports closures as anonymous methods or [[Anonymous function#C#lambda expressions|lambda expressions]] with full-featured [[Closure (computer science)|closure]] semantics.<ref>{{cite web |url=http://msdn.microsoft.com/en-ca/magazine/cc301810.aspx | work = MSDN Magazine |title=An Introduction to Delegates |date=April 2001 |access-date =23 December 2008 | last = Richter | first = Jeffrey |author-link = Jeffrey Richter}}</ref><ref>{{cite web |url=http://diditwith.net/PermaLink,guid,235646ae-3476-4893-899d-105e4d48c25b.aspx | work = Did it with .NET |title=What's in a Closure? | last = Campbell | first = Dustin |date=9 February 2007 |access-date=23 December 2008 |archive-url=https://web.archive.org/web/20140815085318/http://diditwith.net/PermaLink,guid,235646ae-3476-4893-899d-105e4d48c25b.aspx |archive-date=15 August 2014 |url-status=dead |df=dmy-all}}</ref> In Java, anonymous inner classes will remain the preferred way to emulate closures until Java 8 has become the new standard. This is a more verbose construction. This approach also has some differences compared to real closures, notably more controlled access to variables from the enclosing scopes: only final members can be referenced. Java 8, however introduces lambdas that fully inherit the current scope and, in fact, do not introduce a new scope. When a reference to a method can be passed around for later execution, a problem arises about what to do when the method has references to variables/parameters in its lexical scope. C# closures can access any variable/parameter from its lexical scope. In Java's anonymous inner classes, only references to final members of the lexical scope are allowed, thus requiring the developer to mark which variables to make available, and in what state (possibly requiring boxing).
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)