Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Syntax lambdas with curly braces

Tags:

syntax

c#

lambda

delegate int AddDelegate(int a, int b);
AddDelegate ad = (a,b) => a+b;


AddDelegate ad = (a, b) => { return a + b; };

The two above versions of AddDelegate are equivalent. Syntactically, why is it necessary to have a semicolon before and after the } in the second AddDelegate? You can a compiler error ; expected if either one is missing.

like image 956
wootscootinboogie Avatar asked Dec 01 '22 17:12

wootscootinboogie


1 Answers

Maybe this will make it clearer:

AddDelegate ad = (a, b) =>
                 {
                     return a + b;
                 };

These semicolons effectively are for different lines.

like image 111
Andrei Avatar answered Dec 04 '22 06:12

Andrei