can someone explain a delegate
In my time using C#, I’ve never used
delegate
in my own code unless it was in a premade function. What does it really even do? Code examples would be nice too. I’ve tried googling but I don’t understand still :/27 Replies
You're familiar with C, right?
A delegate is basically just a fancy function pointer. When you declare a delegate, you give it a signature, just like a method, and you can invoke delegate instances just like methods with that signature.
Do ya have a code example bc my brain doesn’t like words 😭
It essentially allows you to store methods inside variables
What purpose does it serve
Ohh can I reassign the variable to a different function?
yep
As long as it has the same signature
So what applicable purpose does this have
Right. So in C# there's a built-in library called
System.Linq
which is a set of methods for working with collections. It includes methods for transforming collections, filtering collections, etc., which heavily utilizes delegates.Thinker
REPL Result: Success
Console Output
Compile: 464.084ms | Execution: 92.944ms | React with ❌ to remove this embed.
Like this for instance ^
Here we create an array of integers, then use the
Where
method to filter for all the even numbers by passing the IsEven
method to the Where
method.you can also assign methods for the event pattern
Does it allow me to pass methods as args into other methods
wdym?
Like if it’s a variable
Can I make a method, that takes a delegate signature of some sort as an argument
You can't take a signature as an argument
My poor brain 😭
Like if you declare a delegate then you can call the delegate by passing it two integers, and it'll return an integer.
But why can’t you call it normally
And not use delegates
I’m sorry my brain isn’t functioning
Because the concept of passing a function to another function is very very useful
Like in this example, we're passing
IsEven
to Where
which in turn calls the IsEven
method on every element of the array to determine what elements to leave out.
Sorta like
We give Where
a function and it calls that function on every elementSo if I have a single function
I can make it do different things
But with the same code
Because when you pass the delegate variable as an argument
You can call the variable and get different answers
But each function has to have the same number of arguments
public delegate bool IsThis(int num);
And then I could make something like
static bool Func1(int num)
static bool Func2(int num)
Both do different things
And if I have a function that does this
yep
OHHH I GET IT
FINALLY
omg it really is like a c pointer qwq
It's like, you can assign an int variable any integer, the difference is just that you're assigning the variable any function which looks like that.
Ahhh
Okay
So one function could return true
And other false
But the main function would handle it from then?
I’m on my phone so Gimmie a second to boot Replit to try
I get it now
Thank you fluffy goober pet pat
It's essentially giving a name to a function signature (signature being the return value data type + the parameters, as in how many parameters, what datatype they are, etc).
Oh sorry, you had already understood I didnt scroll all the way down lol
Lol it’s okay