I want to know which of these two FiniteStateMachine.AddState()
methods is better than the other, for example, does one require unboxing/boxing? or do both require unboxing/boxing? and if any of them is better than the other, which one?
public interface IState
{
string Name { get; set; }
}
public sealed class FiniteStateMachine
{
private Dictionary<string, IState> allStates = new Dictionary<string, IState>();
public void AddState<TState>(TState newState) where TState : IState
{
allStates.Add(newState.Name, newState);
}
public void AddState(IState newState)
{
allStates.Add(newState.Name, newState);
}
}
public class EnemySoldierSword_TakingHitState : IState
{
public string Name { get; set; }
public EnemySoldierSword_TakingHitState(string name)
{
Name = name;
}
}
public class Program
{
public var FSM = new FiniteStateMachine();
FSM.AddState(new EnemySoldierSword_ChasingState("ChasingState"));
}
Since IState
is a reference type you cannot avoid boxing so the only difference is where the boxing occurs. If TState
is a value type it will occur when calling allStates.Add
otherwise it will happen when calling the AddState
method.