❔ Understanding Delegates

Hi there. I'm a bit new to delegates. There are a couple things that I would like to straighten out. For one, I don't exactly know what they are, but do I know how they can be used. I know how to create one, but I do not know their limitations nor how to subscribe with the question mark (?). Also, I have another thought about the use case of a Delegate, but I'm not sure if the way I'm thinking about it can be used this way.
/*This is an example of "can I use a Delegate this way as well"*/

private int Health, MagicPoints, ExperiencePoints;

private delegate int StatBars(int Hp, int Mp, int Xp);

private void Start()
{
PassExample(Health);
}
private void PassExample(StatBars myStats)
{
myStats(Health, MagicPoints, ExperiencePoints);
}

private void Health(int hp, int mp, int xp)
{
hp = Health = 25;
mp = MagicPoints = 50;
xp = ExperiencePoints = 0;
}
/*This is an example of "can I use a Delegate this way as well"*/

private int Health, MagicPoints, ExperiencePoints;

private delegate int StatBars(int Hp, int Mp, int Xp);

private void Start()
{
PassExample(Health);
}
private void PassExample(StatBars myStats)
{
myStats(Health, MagicPoints, ExperiencePoints);
}

private void Health(int hp, int mp, int xp)
{
hp = Health = 25;
mp = MagicPoints = 50;
xp = ExperiencePoints = 0;
}
When writing it out, it looks a tad improper but I am unaware of if this is acceptable.
6 Replies
Angius
Angius2y ago
A delegate, in this context, is nothing more than a type for a method. Be it a regular method, or a lambda
int Foo(Func<int, int> func)
{
return func(69);
}
int Foo(Func<int, int> func)
{
return func(69);
}
and
delegate int FooFunc(int param);

int Foo(FooFunc func)
{
return func(69);
}
delegate int FooFunc(int param);

int Foo(FooFunc func)
{
return func(69);
}
are basically the same In the second case, though, you get proper naming for the parameters, better IDE hints, and it's easier to see what's the return type and what's the param type
anita
anita2y ago
I also recommend to use func, action. It is way easier to understand imo
adamistheman
adamisthemanOP2y ago
So to reiterate, writing this down would look like this instead? :
// I shortened this down to just 1 int.

private int Health;
private delegate int HealthBar(int Hp);

private void PassExample(StatBars myStats)
{
myStats(Health);
}

HealthBar hpBar = HP => Hp.Health;
// I shortened this down to just 1 int.

private int Health;
private delegate int HealthBar(int Hp);

private void PassExample(StatBars myStats)
{
myStats(Health);
}

HealthBar hpBar = HP => Hp.Health;
Would this be right? And I can also expand upon it with what I have in my previous example?
Angius
Angius2y ago
HP => Hp.Health this won't work, HP and Hp are different variables And you never call PassExample()
Anton
Anton2y ago
are you aiming for like a getter of the hp amount in form of a delegate?
public delegate int HpGetter();
public class A
{
private int _hp;
public HpGetter HpGetter => (() => _hp);
// or just
public int GetHp() => _hp;
}

// ...
void Stuff(A a, HpDisplay hp)
{
HoGetter hpGetter = a.HpGetter;
// or
HpGetter hpGetter = a.GetHp;
// or
HpGetter hpGetter = () => a.GetHp();

hp.Display(hpGetter);
}
public delegate int HpGetter();
public class A
{
private int _hp;
public HpGetter HpGetter => (() => _hp);
// or just
public int GetHp() => _hp;
}

// ...
void Stuff(A a, HpDisplay hp)
{
HoGetter hpGetter = a.HpGetter;
// or
HpGetter hpGetter = a.GetHp;
// or
HpGetter hpGetter = () => a.GetHp();

hp.Display(hpGetter);
}
kinda like this think of delegates as interfaces with one method
interface IHpGetter
{
int Hp { get; }
}
class A
{
private int _hp;
private class _HpGetter : IHpGetter
{
private A _a;
public int Hp => _a._hp;
}
public IHpGetter HpGetter => new _HpGetter(this);
}
interface IHpGetter
{
int Hp { get; }
}
class A
{
private int _hp;
private class _HpGetter : IHpGetter
{
private A _a;
public int Hp => _a._hp;
}
public IHpGetter HpGetter => new _HpGetter(this);
}
this is very close to what a delegate does internally
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server