If you're just learning C# you've probably come across something like this and wondered what the weird x=>x is all about...
things.SingleOrDef...
For further actions, you may consider blocking this person and/or reporting abuse
If anyone's coming from "JavaScript" background, you will find that it works very similar to the JavaScript's lambda.
To show the similarity (using the examples in this article)
1 - You can leave out
()
if you have only one argument passed.2 - You can pass the method name
Both lambda syntax are the same in C# & JavaScript.
You can go one step further:
things.SingleOrDefault(({name}) => name === "Bob");
I wasn't able to replicate the object desconstruction in C# as you could do in JavaScript (even after implementing Deconstruct)
dotnetfiddle.net/Wbx3ac
And Roslyn 2.0 compiler complains...
Sorry, I was talking explicitly on JS terms
No worries, mate~ 😉
I think it's worth pointing out that a lambda is not simply a shorthand for function declaration.
IsAmy
is a function. The other two variants are both lambdas and they differ from functions in that they capture all the locals and globals that are visible in the scope where the lambda is defined.IsAmy
has no access to those.Thanks for this, an important clarification and I appreciate you taking the time to outline it here :-)