Home >Backend Development >C++ >Are delegate keywords and lambda notation functionally identical in C#?
Delegate Keyword vs. Lambda Notation
In C#, a common question arises regarding the equivalence of two lambda expressions:
<code class="csharp">delegate { x = 0; } () = > { x = 0 }</code>
Are they functionally identical once compiled?
Answer:
In short, no. They are syntactically equivalent, but differ in the resulting delegate type.
Delving Deeper:
While the delegate keyword traditionally creates anonymous delegates, using a lambda with the delegate keyword results in an anonymous delegate as well. However, assigning a lambda to an Expression type creates an expression tree instead, which can subsequently be compiled to an anonymous delegate.
Advanced Considerations:
The choice between these notations depends on the intended usage:
The above is the detailed content of Are delegate keywords and lambda notation functionally identical in C#?. For more information, please follow other related articles on the PHP Chinese website!