C
C#11mo ago
n

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
No description
20 Replies
Omnissiah
Omnissiah11mo ago
the lambda type is compatible with the delegate, it's just Action, so why should there be an issue?
n
nOP11mo ago
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
Omnissiah
Omnissiah11mo ago
because types can be automatically deduced
n
nOP11mo ago
is that only with lambda? because when i remove the delegate type from the first statement it throws an error
Omnissiah
Omnissiah11mo ago
what do you mean, remove how (also you can paste the $code here) not working 😐 $code
MODiX
MODiX11mo ago
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/
n
nOP11mo ago
so this is correct
// anonymous method
TestDelegate testDelegate = delegate () { Console.WriteLine("Hello!"); };

// anonymous method with lambda expression
testDelegate += () => { Console.WriteLine("Hello 2!"); };
// anonymous method
TestDelegate testDelegate = delegate () { Console.WriteLine("Hello!"); };

// anonymous method with lambda expression
testDelegate += () => { Console.WriteLine("Hello 2!"); };
but when i remove delegate from the top line
// anonymous method
TestDelegate testDelegate = () { Console.WriteLine("Hello!"); };

// anonymous method with lambda expression
testDelegate += () => { Console.WriteLine("Hello 2!"); };
// anonymous method
TestDelegate testDelegate = () { Console.WriteLine("Hello!"); };

// anonymous method with lambda expression
testDelegate += () => { Console.WriteLine("Hello 2!"); };
it doesnt like that, but the line with the lambda expression doesnt have it
Vi Ness
Vi Ness11mo ago
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 lambda
n
nOP11mo ago
I 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
Omnissiah
Omnissiah11mo ago
this TestDelegate testDelegate = () { Console.WriteLine("Hello!"); }; is not correct like it's not valid syntax
n
nOP11mo ago
so 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
Omnissiah
Omnissiah11mo ago
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 method1 = delegate () { };
var method2 = () => { };
var method1 = delegate () { };
var method2 = () => { };
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
n
nOP11mo ago
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
Omnissiah
Omnissiah11mo ago
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 meaning
n
nOP11mo ago
I 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
Omnissiah
Omnissiah11mo ago
lambda syntax is more modern, essentially that's the one to use do have a less clunky code
Thinker
Thinker11mo ago
Literally no one uses the delegate () {} syntax anymore, it's incredibly old
Roan
Roan11mo ago
public event EventHandler NewEvent; <--- this automatically creates a delegate for you to! public event EventHandler<CustomEventArgs> NewEvent; <--- For when you need custom args.
blinkbat
blinkbat11mo ago
TestDelegate testDelegate = () { Console.WriteLine("Hello!"); }; this isn't valid btw
Omnissiah
Omnissiah11mo ago
how would the compiler know how to interpret that you need something more
Want results from more Discord servers?
Add your server