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
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 thingsI 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
if you care about performance there are better ways to abstract than delegate just fyi
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
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+Ah yeah, looking up "linq delegate methods" was a much better keyword than just looking up "c# delegates" lol
https://www.c-sharpcorner.com/UploadFile/tirthacs/func-delegates-in-linq/
https://www.codingame.com/playgrounds/345/c-linq-background-topics/delegate-variables
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
What other good google keywords are there for looking up info on delegates?@ZacharyPatten
Enumerable Class (System.Linq)
Provides a set of static (Shared in Visual Basic) methods for querying objects that implement IEnumerable.
litereally just go read the docs
as mentioned
System.Linq
is full of delegate usageFunc 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
I still don't get the significance of the signature
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 Predicateaside 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
Oh the first line of the method is the signature
https://www.c-sharpcorner.com/UploadFile/puranindia/method-signatures-in-C-Sharp/
Method Signatures in C#
In this article I will explain you about Method Signatures in C#.
Smh my heads
Yes, it defines the general input and output
You need to define your own delegate types if you want ref and out parameters
Ohhh
But most of the time you can get away with Func and Action
Ive been using methods for half a month without knowing that the first line was called a signature
Action for void where all the generics are parameters. Predicate is like action but defines bool as the return instead of void