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
Thinker
Thinker13mo ago
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.
🧵Stellar 🪡
🧵Stellar 🪡OP13mo ago
Do ya have a code example bc my brain doesn’t like words 😭
Thinker
Thinker13mo ago
delegate int MyDelegate(int a, int b); // Create a delegate with the signature int(int a, int b)

static int Add(int a, int b) => a + b; // Create a static method

MyDelegate myDelegate = Add; // Assign a varaible of the delegate type to the method
int result = myDelegate(1, 2); // Call the delegate as if it was the Add method
Console.WriteLine(result); // Write the result
delegate int MyDelegate(int a, int b); // Create a delegate with the signature int(int a, int b)

static int Add(int a, int b) => a + b; // Create a static method

MyDelegate myDelegate = Add; // Assign a varaible of the delegate type to the method
int result = myDelegate(1, 2); // Call the delegate as if it was the Add method
Console.WriteLine(result); // Write the result
It essentially allows you to store methods inside variables
🧵Stellar 🪡
🧵Stellar 🪡OP13mo ago
What purpose does it serve Ohh can I reassign the variable to a different function?
Thinker
Thinker13mo ago
yep As long as it has the same signature
🧵Stellar 🪡
🧵Stellar 🪡OP13mo ago
So what applicable purpose does this have
Thinker
Thinker13mo ago
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.
MODiX
MODiX13mo ago
Thinker
REPL Result: Success
using System.Linq;

static bool IsEven(int x) => x % 2 == 0;

int[] integers = [1, 2, 3, 4, 5];

var even = integers.Where(IsEven);

foreach (var x in even) Console.WriteLine(x);
using System.Linq;

static bool IsEven(int x) => x % 2 == 0;

int[] integers = [1, 2, 3, 4, 5];

var even = integers.Where(IsEven);

foreach (var x in even) Console.WriteLine(x);
Console Output
2
4
2
4
Compile: 464.084ms | Execution: 92.944ms | React with ❌ to remove this embed.
Thinker
Thinker13mo ago
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.
FestivalDelGelato
you can also assign methods for the event pattern
🧵Stellar 🪡
🧵Stellar 🪡OP13mo ago
Does it allow me to pass methods as args into other methods
Thinker
Thinker13mo ago
wdym?
🧵Stellar 🪡
🧵Stellar 🪡OP13mo ago
Like if it’s a variable Can I make a method, that takes a delegate signature of some sort as an argument
Thinker
Thinker13mo ago
You can't take a signature as an argument
🧵Stellar 🪡
🧵Stellar 🪡OP13mo ago
My poor brain 😭
Thinker
Thinker13mo ago
Like if you declare a delegate
delegate int MyDelegate(int a, int b);
delegate int MyDelegate(int a, int b);
then you can call the delegate by passing it two integers, and it'll return an integer.
🧵Stellar 🪡
🧵Stellar 🪡OP13mo ago
But why can’t you call it normally And not use delegates I’m sorry my brain isn’t functioning
Thinker
Thinker13mo ago
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
int[] integers = [
1,
2,
3,
4,
5
];

// this obviously doesn't work but it's just for demonstration
int[] even = [
IsEven(1),
IsEven(2),
IsEven(3),
IsEven(4),
IsEven(5)
];
int[] integers = [
1,
2,
3,
4,
5
];

// this obviously doesn't work but it's just for demonstration
int[] even = [
IsEven(1),
IsEven(2),
IsEven(3),
IsEven(4),
IsEven(5)
];
We give Where a function and it calls that function on every element
🧵Stellar 🪡
🧵Stellar 🪡OP13mo ago
So 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
Thinker
Thinker13mo ago
yep
🧵Stellar 🪡
🧵Stellar 🪡OP13mo ago
OHHH I GET IT FINALLY omg it really is like a c pointer qwq
Thinker
Thinker13mo ago
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.
🧵Stellar 🪡
🧵Stellar 🪡OP13mo ago
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
🧵Stellar 🪡
🧵Stellar 🪡OP13mo ago
I get it now
No description
No description
🧵Stellar 🪡
🧵Stellar 🪡OP13mo ago
Thank you fluffy goober pet pat
Gipper
Gipper13mo ago
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
🧵Stellar 🪡
🧵Stellar 🪡OP13mo ago
Lol it’s okay

Did you find this page helpful?