C
C#2y ago
Velcer

❔ Any Danger to using Actions in this way?

public class Entity
{
public event Action<int> OnUpdateFrame;

internal void OnNewTick(int tick)
{
OnUpdateFrame?.Invoke(tick);
}
}

public class Frog : Entity
{
public Frog()
{
OnUpdateFrame += Frog_OnUpdateFrame;
}

private void Frog_OnUpdateFrame(int obj)
{
throw new NotImplementedException();
}
}

public class SuperFrog : Frog
{
public SuperFrog() : base()
{
OnUpdateFrame += SuperFrog_OnUpdateFrame;
}

private void SuperFrog_OnUpdateFrame(int obj)
{
throw new NotImplementedException();
}
}
public class Entity
{
public event Action<int> OnUpdateFrame;

internal void OnNewTick(int tick)
{
OnUpdateFrame?.Invoke(tick);
}
}

public class Frog : Entity
{
public Frog()
{
OnUpdateFrame += Frog_OnUpdateFrame;
}

private void Frog_OnUpdateFrame(int obj)
{
throw new NotImplementedException();
}
}

public class SuperFrog : Frog
{
public SuperFrog() : base()
{
OnUpdateFrame += SuperFrog_OnUpdateFrame;
}

private void SuperFrog_OnUpdateFrame(int obj)
{
throw new NotImplementedException();
}
}
5 Replies
x0rld
x0rld2y ago
what is Frog_OnUpdateFrame ? you should have OnUpdateFrame protected to not allow external access
Velcer
Velcer2y ago
Thank you. So I want to use Actions to let higher level classes subscribe to the update method. I want to know if this is potentially going to hurt the performance of my code versus just using overrides.
x0rld
x0rld2y ago
you can declare it virtual protected in your entity you can do a minimal usage and test the 2 way with Benchmark.NET
lycian
lycian2y ago
if you're only going to do one handler for the event, a virtual method would be best
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
More Posts