First time using delegates/lambda expressions
Hi, I've watched countless videos on delegates and lambda expressions but I can't really seem to get my head around them. I'm currently playing around with them and I'm wondering why is this second testDelegate not throwing an error without defining it's type as a delegate? The only difference is that the second one uses a lambda expression to assign it.
If someone could give me a little explanation on why this is, that'd be great. I sort of understand how delegates work - they point to a function and this can be changed/added to, so that it runs a different function or more than 1 function each time it is called. Lambda expressions just confuse me however, all I've got from these videos is that it is referred to as a "go to" expression and can be used in LINQ functions like .Where and .OrderBy to sort lists and such
20 Replies
the lambda type is compatible with the delegate, it's just
Action
, so why should there be an issue?No i'm not saying there's an issue, I'm wondering why what I wrote works
why it's correct
and why i don't have to use the type delegate
because types can be automatically deduced
is that only with lambda? because when i remove the delegate type from the first statement it throws an error
what do you mean, remove how
(also you can paste the $code here)
not working 😐
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/so this is correct
but when i remove delegate from the top line
it doesnt like that, but the line with the lambda expression doesnt have it
If you remove the
=>
it doesn't like that either. It looks like it's just two different ways of doing the same thing. This is the first time I've seen using the delegate keyword to create an anonymous method instead of a lambdaI see, I think the video I was watching first did it using the delegate keyword to show what it is doing in more simple terms then
this
TestDelegate testDelegate = () { Console.WriteLine("Hello!"); };
is not correct
like it's not valid syntaxso what exactly is the lamba expression doing in the second line that the first line isnt doing
i don't fully understand lambda expressions yet
the delegate keyword enables a syntax to define inline/anonymous methods
or declare a signature
the lambda => is a syntax to declare an inline method
if you look at the resulting type, probably both of them ends in an Action
so these two
var is Action in both cases
other features, like static, are present in both cases
i believe context is captured in the same way too but i should check honestly
and if you look at the definition of Action, it is defined as a delegate
I kind of see what you're saying
i might need to jump back to something simpler if i want to understand this though because i still can't really get my head around it
thank you for your help though
also i believe this
testDelegate += () => { Console.WriteLine("Hello 2!"); };
technically is a MulticastDelegate now
and also there is the complication of the MethodGroup, because you can have method that is overloaded
just as a reference in case you return to this
anyway as long as a lambda is compatible with a delegate you will receive no warnings when you pass to one or the other
although it seems that your difficulty is more with the syntax than the meaningI think it's a bit of both, and more of like when to actually use the lambda expression, I think it'll come with more practice though
and yeah that was a multicast delegate, I never thought about it using overloaded methods though that's interesting
lambda syntax is more modern, essentially that's the one to use do have a less clunky code
Literally no one uses the
delegate () {}
syntax anymore, it's incredibly oldpublic event EventHandler NewEvent; <--- this automatically creates a delegate for you to!
public event EventHandler<CustomEventArgs> NewEvent; <--- For when you need custom args.
TestDelegate testDelegate = () { Console.WriteLine("Hello!"); };
this isn't valid btwhow would the compiler know how to interpret that
you need something more