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!
=== Delegates, method references === C# implements object-oriented method pointers in the form of [[Delegate (CLI)|delegates]]. A delegate is a special type that can capture a reference to a method. This reference can then be stored in a delegate-type variable or passed to a method through a delegate parameter for later invocation. C# delegates support [[Covariance and contravariance (computer science)|covariance and contravariance]], and can hold a reference to any signature-compatible static method, instance method, anonymous method or [[Anonymous function|lambda expression]]. Delegates should not be confused with closures and inline functions. The concepts are related because a reference to a closure/inline function must be captured in a delegate reference to be useful at all. But a delegate does not always reference an inline function; it can also reference existing static or instance methods. Delegates form the basis of C# [[#Event|events]], but should not be confused with those either. Delegates were deliberately left out of Java because they were considered unnecessary and detrimental to the language, and because of potential performance issues.<ref name="JavaDelegates">{{cite web|url=http://java.sun.com/docs/white/delegates.html |title=Oracle Technology Network for Java Developers | Oracle Technology Network | Oracle |publisher=Java.sun.com |access-date=24 February 2015 |url-status=dead |archive-url=https://web.archive.org/web/20120627043929/http://java.sun.com/docs/white/delegates.html |archive-date=27 June 2012 }}</ref> Instead, alternative mechanisms are used. The [[wrapper pattern]], which resembles the delegates of C# in that it allows the client to access one or more client-defined methods through a known [[Interface (object-oriented programming)|interface]], is one such mechanism.{{citation needed |date=March 2012}} Another is the use of [[Adapter pattern|adapter]] objects using inner classes, which the designers of Java argued are a better solution than bound method references.<ref name="JavaDelegates"/> See also example [[#C# delegates and equivalent Java constructs|C# delegates and equivalent Java constructs]].
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)