C
C#2y ago
surwren

C delegates

C# delegates: I know that they're pointers to methods but I don't fully understand them, how they word, or what the point of their existence is. Why would one need to use anonymous methods in a program?
21 Replies
ZacharyPatten
ZacharyPatten2y ago
there are many reasons there are honestly so many reasons for where and why to use them it is hard to even start to answer your question have you used any of the extension methods in System.Linq? many of the extension methods in System.Linq use delegates and are very common to see in C# code also, a "delegate" and an "anonymous method" are two different things
Callum
Callum2y ago
I used delegates to abstract functions with unknown parameters As I needed to store a set of abstract functions to be called when an event gets fired Very niche but quite handy
ZacharyPatten
ZacharyPatten2y ago
if you care about performance there are better ways to abstract than delegate just fyi
surwren
surwren2y ago
I'm completely lost when it comes to delegates. Are there good resources describing them? I don't really grasp their significance from the microsoft documentation either
ZacharyPatten
ZacharyPatten2y ago
you just need experience as mentioned you shoudl really check out the methods in System.Linq they are extremely common so common that they decided to include Linq in the implicit namespace feature in .NET 6+
surwren
surwren2y ago
Ah yeah, looking up "linq delegate methods" was a much better keyword than just looking up "c# delegates" lol
surwren
surwren2y ago
Func<> Delegates in LINQ
Learn how Func<> Delegates in LINQ work. Func delegates are pointers to methods that take one or more parameters and must return a value.
CodinGame
Delegate Variables - C# LINQ Background Topics
Explore this playground and try new concepts right into your browser
surwren
surwren2y ago
What other good google keywords are there for looking up info on delegates?@ZacharyPatten
ZacharyPatten
ZacharyPatten2y ago
Enumerable Class (System.Linq)
Provides a set of static (Shared in Visual Basic) methods for querying objects that implement IEnumerable.
ZacharyPatten
ZacharyPatten2y ago
litereally just go read the docs as mentioned System.Linq is full of delegate usage
TheBoxyBear
TheBoxyBear2y ago
Func is easier to understand because its a built in type. The next step is fully understanding delegates is looking at the delegate keyword and defining your own types Delegate types define a signature so when you declare an object as that type, it becomes a method pointer with that signature
surwren
surwren2y ago
I still don't get the significance of the signature
TheBoxyBear
TheBoxyBear2y ago
It defines a methods paramets and return type with it defining names to the method or the parameters Like void(int) Though with delegates you do give names to parameters delegate void MyDelegate(int n) MyDelegate del = someVoidMethodThatTakesInt The delegate type defines that methods can be used as values because the input and outputs have to match (the signature) Func is just a delegate type that is predefined .NET. Same for Action and Predicate
ZacharyPatten
ZacharyPatten2y ago
aside from reading docs you should learn a functional programming language that will not only help you learn delegates but it will teach you other patterns in programming I had to learn prolog when I was in college prolog sucks but I learned a lot I wouldn't recommend prolog for you to learn though but there are multiple popular functional programming languages out there
surwren
surwren2y ago
Method Signatures in C#
In this article I will explain you about Method Signatures in C#.
surwren
surwren2y ago
Smh my heads
TheBoxyBear
TheBoxyBear2y ago
Yes, it defines the general input and output You need to define your own delegate types if you want ref and out parameters
surwren
surwren2y ago
Ohhh
TheBoxyBear
TheBoxyBear2y ago
But most of the time you can get away with Func and Action
surwren
surwren2y ago
Ive been using methods for half a month without knowing that the first line was called a signature
TheBoxyBear
TheBoxyBear2y ago
Action for void where all the generics are parameters. Predicate is like action but defines bool as the return instead of void