Anonymous function: Difference between revisions

Content deleted Content added
Completed the list of categories for example code
Filled in 1 bare reference(s) with reFill 2
Line 736:
 
=== C# ===
In [[C Sharp (programming language)|C#]], support for anonymous functions has deepened through the various versions of the language compiler. The language v3.0, released in November 2007 with [[.NET Framework]] v3.5, has full support of anonymous functions.<ref name="Skeet">{{cite book |last=Skeet|first=Jon|title= C# in Depth |date=23 March 2019 |publisher= Manning |isbn= 978-1617294532}}</ref>{{rp|7-8}}<ref name="Albahari">{{cite book |last=Albahari |first=Joseph |title= C# 10 in a Nutshell |year=2022 |publisher= O'Reilly |isbn= 978-1-098-12195-2}}</ref>{{rp|26}} C# names them ''lambda expressions'', following the original version of anonymous functions, the [[lambda calculus]].<ref>{{Cite web|url=https://www.microsoft.com/en-us/download/details.aspx?id=7029|title=C# Language Specification 5.0|website=Microsoft Download Center}}</ref><ref name="Skeet>{{cite book |last=Skeet|first=Jon|title= C# in Depth |date=23 March 2019 |publisher= Manning |isbn= 978-1617294532}}<"/ref>{{rp|7-8, 91}}<ref name="Albahari>{{cite book |last=Albahari |first=Joseph |title= C# 10 in a Nutshell |year=2022 |publisher= O'Reilly |isbn= 978-1-098-12195-2}}<"/ref>{{rp|91}}
 
''// the first int is the x' type''
Line 744:
<syntaxhighlight lang="csharp" inline>Console.WriteLine(foo(7));</syntaxhighlight>
 
While the function is anonymous, it cannot be assigned to an implicitly typed variable, because the lambda syntax may be used for denoting an anonymous function or an expression tree, and the choice cannot automatically be decided by the compiler.<ref name="Skeet>{{cite book |last=Skeet|first=Jon|title= C# in Depth |date=23 March 2019 |publisher= Manning |isbn= 978-1617294532}}<"/ref>{{rp|101-103}} E.g., this does not work:
<syntaxhighlight lang="csharp">
// will NOT compile!
Line 759:
</syntaxhighlight>
 
Prior versions of C# had more limited support for anonymous functions. C# v1.0, introduced in February 2002 with the .NET Framework v1.0, provided partial anonymous function support through the use of [[Delegate (CLI)|delegates]].<ref name="Skeet>{{cite book |last=Skeet|first=Jon|title= C# in Depth |date=23 March 2019 |publisher= Manning |isbn= 978-1617294532}}<"/ref>{{rp|6}} C# names them ''lambda expressions'', following the original version of anonymous functions, the [[lambda calculus]].<ref name="Skeet>{{cite book |last=Skeet|first=Jon|title= C# in Depth |date=23 March 2019 |publisher= Manning |isbn= 978-1617294532}}<"/ref>{{rp|91}} This construct is somewhat similar to PHP delegates. In C# 1.0, delegates are like function pointers that refer to an explicitly named method within a class. (But unlike PHP, the name is unneeded at the time the delegate is used.) C# v2.0, released in November 2005 with the .NET Framework v2.0, introduced the concept of anonymous methods as a way to write unnamed inline statement blocks that can be executed in a delegate invocation.<ref name="Skeet>{{cite book |last=Skeet|first=Jon|title= C# in Depth |date=23 March 2019 |publisher= Manning |isbn= 978-1617294532}}<"/ref>{{rp|6-7}} C# 3.0 continues to support these constructs, but also supports the lambda expression construct.
 
This example will compile in C# 3.0, and exhibits the three forms:
Line 799:
</syntaxhighlight>
 
In the case of the C# 2.0 version, the C# compiler takes the code block of the anonymous function and creates a static private function. Internally, the function gets a generated name, of course; this generated name is based on the name of the method in which the Delegate is declared. But the name is not exposed to application code except by using [[Reflection (computer science)|reflection]].<ref name="Skeet>{{cite book |last=Skeet|first=Jon|title= C# in Depth |date=23 March 2019 |publisher= Manning |isbn= 978-1617294532}}<"/ref>{{rp|103}}
In the case of the C# 3.0 version, the same mechanism applies.
 
Line 1,059:
</syntaxhighlight>
 
The main difference here is that the lambda expression does not necessarily need to allocate a new instance for the {{code|IntegerMath}}, and can return the same instance every time this code is run.<ref>{{citeCite web|url=https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.27.4|title=Run-Time Evaluation of Lambda Chapter&nbsp;15.&nbsp;Expressions|website=docs.oracle.com}}</ref>
Additionally, in the [[OpenJDK]] implementation at least, lambdas are compiled to {{mono|invokedynamic}} instructions, with the lambda body inserted as a static method into the surrounding class,<ref>{{cite web |title=jdk/LambdaMethod.java |website=[[GitHub]] |url=https://github.com/openjdk/jdk/blob/master/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java#L329-L334}}</ref> rather than generating a new class file entirely.