❔ Lambda expressions in C#
Hi, I'm afraid I'm not used to lambdas enough and am struggling whenever they pop up.
I need a place to practice lambda expressions (writing my own, as well as using existing ones).
I searched on google but w3schools only has py and java examples
8 Replies
There are no existing ones
Lambdas are there specifically as throwaway single-use functions
If you want to practice them, start some project, and use them instead of normal methods
So if you would otherwise have
replace it with a
and so on
Also, use LINQ
Archival Advent of Code tasks are really cool for learning LINQ, since they're mostly all about pulling needed data from a huge dataset
So instead of using loops and the like, force yourself to use LINQ
I've practiced with LINQ and Java .stream() functional programming methods before, but they aren't the same as doing something like
as well as calling this type of method with lambdas
While I'm aware that this also involves coroutines (which are probably confusing me further), I feel really stumped whenever I see syntax like this. I'd like to be able to read/write this type of stuff on snap reaction rather than taking 20 seconds to brain
There isn't really much to it, though?
Func
if it returns a value
Action
if it doesn't
Order of type params for Func
is Func<TReturn, TParam1, TParam2, ...>
You could try taking existing methods and writing out their signatures
Then see if the method fits iti want to practice writing them, i just don't have exercises to start writing if it maeks sense
Here you go, I asked ChatGPT to generate 20 random method signatures. Try to write them out as
Func
s and Action
s:
You can ask it for more if you need
And then, as an added exercise, try writing a lambda that also fits itoh, I didn't think of GPT
I can probably can ask it for the answers too to check my practice
So, take
try writing out it's signature as
and assign the method to see if it works, then try writing a lambda
that also satisfies the signature
I can't really think of a better exercise, this covers everything
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.