0

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"));
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Lunatrap
  • 15
  • 5

1 Answers1

0

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.

Lee
  • 142,018
  • 20
  • 234
  • 287
  • I see, so there is a difference but that difference does not matters in this context, thanks a lot thats what i wanted to know, thanks – Lunatrap Mar 24 '16 at 00:42