This is very useful: if it were a function we would have to add it as parameter (or use a global variable in languages that support it).
Notice that in C# strings are references so if you later change the value you do it in the lambda too.
Try this code:
varmyName="Karl";stringDelegatesayHello3=(name)=>"Hello "+name+", I'm "+myName+"!";System.Console.WriteLine(sayHello3("Frank"));// we change the value referenced by myNamemyName="Peter";System.Console.WriteLine(sayHello3("Jason"));
Will give you this output:
Hello Frank, I'm Karl!
Hello Jason, I'm Peter!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Lambdas are, in a nutshell, functions. Let's say you have a function like this one:
you can rewrite it like this:
Having defined
stringDelegate
like this:One peculiarity is the variable capture: lambdas can capture - close over - variables in scope during lambda definition. So this works:
This is very useful: if it were a function we would have to add it as parameter (or use a global variable in languages that support it).
Notice that in C# strings are references so if you later change the value you do it in the lambda too.
Try this code:
Will give you this output: