C
C#3mo ago
Faker

✅ Delegates in C#

Hello guys, I just read a bit about delegates in C#, can someone confirm whether the following statements are correct and would really appreciate if you guys can add to those statements if needed (like any things I missed). So, what I've understood, is that a delegate is just a type that stores a reference to a method. if we declare a delegate method like this: public static delegate int PerformCalculations(int x, int y); We are expecting our delegate type to reference a method that will return an int and have 2 parameters of type int. So we expect to reference a method like this: public static int AddNum(int num1, int num2) {...} Then we store the reference to the method as follows: PerformCalculations myRef = AddNum; (I have one question, noticed that we don't use any constructor call when we create the object, what does that mean?) Finally, can someone explain, why is a delegate useful pls
14 Replies
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Faker
FakerOP3mo ago
yep I see yeah will learn that, I wanted to learn anonymous function/lambda functions but I think I needed to learn delegates first, just to have an overview of what it is I have 2 last question. Notice that when we declare a delegate, like PerformCalculations myRef = AddNum, we didn't use a constructor, what does this mean when we don't use a constructor? Also, I read that we can't use the "static" keyword when declaring a delegate method signature, why is it so pls
FusedQyou
FusedQyou3mo ago
A delegate method signature is like defining a class You don't use it directly, you define instances under it You don't use static here because there's no explicit instance to specify, you have to manually specify the method for them, like an alias Kind of like an enum, you don't use it explicitly or in a static context but rather specify a numer as the instance under it. It's an alias to the underlying instance
Faker
FakerOP3mo ago
yeah I see, makes more sense when we think of it like a class, a class can't be static but we can create instances out of it and those instances can have static method or non-static methods for e.g
FusedQyou
FusedQyou3mo ago
A class can be static, I just mean you don't use it directly by default. Maybe that was a bad example Suppose the enum is a better example here
Faker
FakerOP3mo ago
ahhh a class can be static, didn't know that, just google it and read about it yepp I see, thanks !
FusedQyou
FusedQyou3mo ago
Yes, you don't have to explicitly define an instance and just add a static keyword to use a single instance
Cattywampus
Cattywampus3mo ago
it totally depends, there's one important limitation that Actions/Func cant do that the delegate can e.g:
public delegate T SomDelegate(ref T, ref T);

//the built-ins cant do the above
public delegate T SomDelegate(ref T, ref T);

//the built-ins cant do the above
I'm not saying Im disagreeing btw
*Finally, can someone explain, why is a delegate useful pls *
I've not seeing anyone answered this yet, one most common use case is to avoid deep/nested branches another common use case is for observer pattern stuffs
FusedQyou
FusedQyou3mo ago
I mean the delegate type itself is very useful. It's kind of like dynamic, though you have the ability to fully dissect the method passed so you can have some very complex logic made very easy because you don't have a specific signature requirement Like a Discord bot and its commands, or an api and its endpoints. It can take any amount of parameters and return any type. Implementing a simple flow for it is mad every easy when the input just accepts a delegate and the code dissects the requirements in the background to figure out if it's valid, instead of adding a bunch of signatures that ight match
Cattywampus
Cattywampus3mo ago
also the gap is a bit wider now that we have both *delegate managed/unmanaged <T, T> aka function pointers
(I have one question, noticed that we don't use any constructor call when we create the object, what does that mean?)
nobody answered that too it seems, so here it is : when we do :
static Action<T> Foo;

Foo = MyCLassMethodA;

//Later on you can execute it like so
Foo(T paramA);
static Action<T> Foo;

Foo = MyCLassMethodA;

//Later on you can execute it like so
Foo(T paramA);
that's just assigning 1 specific method with the correct signature to that one delegate, its called SingleCast now for multicasts you must use the += operator so it will be added to the backing array of the delegate
static Action<T> Foo;

Foo += MyCLassMethodA;

//Later on you can invoke them like so
Foo?.Invoke(); // without regular method calling, instead we use Invoke
//the ? might be necessary to check for nulls, better just use it

//Always unsubscribe when you're done with multicast to avoid leaks
Foo -= MyCLassMethodA;

//The -= operator is to remove from the backing array
static Action<T> Foo;

Foo += MyCLassMethodA;

//Later on you can invoke them like so
Foo?.Invoke(); // without regular method calling, instead we use Invoke
//the ? might be necessary to check for nulls, better just use it

//Always unsubscribe when you're done with multicast to avoid leaks
Foo -= MyCLassMethodA;

//The -= operator is to remove from the backing array
Faker
FakerOP3mo ago
Yeah I see, thanks !
Cattywampus
Cattywampus3mo ago
you can tag this thread as solved if you think we all already answered your questions
Cattywampus
Cattywampus3mo ago
or proly @TeBeCo can help to do that, so i wont have this thing annoying me all the time :when:
No description
Faker
FakerOP3mo ago
I will do it 😂

Did you find this page helpful?