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
Thinker11mo 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.
~✰ 𝒮𝓉𝑒𝓁𝓁𝒶𝓇 ✰~
Do ya have a code example bc my brain doesn’t like words 😭
Thinker
Thinker11mo 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
~✰ 𝒮𝓉𝑒𝓁𝓁𝒶𝓇 ✰~
What purpose does it serve Ohh can I reassign the variable to a different function?
Thinker
Thinker11mo ago
yep As long as it has the same signature
~✰ 𝒮𝓉𝑒𝓁𝓁𝒶𝓇 ✰~
So what applicable purpose does this have
Thinker
Thinker11mo 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
MODiX11mo 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
Thinker11mo 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.
Omnissiah
Omnissiah11mo ago
you can also assign methods for the event pattern
~✰ 𝒮𝓉𝑒𝓁𝓁𝒶𝓇 ✰~
Does it allow me to pass methods as args into other methods
Thinker
Thinker11mo ago
wdym?
~✰ 𝒮𝓉𝑒𝓁𝓁𝒶𝓇 ✰~
Like if it’s a variable Can I make a method, that takes a delegate signature of some sort as an argument
Thinker
Thinker11mo ago
You can't take a signature as an argument
~✰ 𝒮𝓉𝑒𝓁𝓁𝒶𝓇 ✰~
My poor brain 😭
Thinker
Thinker11mo 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.
~✰ 𝒮𝓉𝑒𝓁𝓁𝒶𝓇 ✰~
But why can’t you call it normally And not use delegates I’m sorry my brain isn’t functioning
Thinker
Thinker11mo 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
~✰ 𝒮𝓉𝑒𝓁𝓁𝒶𝓇 ✰~
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
Thinker11mo ago
yep
~✰ 𝒮𝓉𝑒𝓁𝓁𝒶𝓇 ✰~
OHHH I GET IT FINALLY omg it really is like a c pointer qwq
Thinker
Thinker11mo 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.
~✰ 𝒮𝓉𝑒𝓁𝓁𝒶𝓇 ✰~
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
~✰ 𝒮𝓉𝑒𝓁𝓁𝒶𝓇 ✰~
Thank you fluffy goober pet pat
Gipper
Gipper11mo 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
~✰ 𝒮𝓉𝑒𝓁𝓁𝒶𝓇 ✰~
Lol it’s okay
Want results from more Discord servers?
Add your server