运算符不能应用于通用类型的操作数

Operator cannot be applied to operands of generic type

我正在用 C# 编写一个分层状态机,并使用一个泛型类型 S,其中将为不同的状态机提供一个具有不同状态的枚举。我收到以下错误。检查代码中的注释。

public interface IStateMachine
{
    public void fireTrigger(GameWideTrigger trigger);
}

public class StateTransition<T, S>
{
    public S StartState { get; private set; }
    public T Trigger { get; private set; }
    public S EndState { get; private set; }
}

public class StateMachine<S> : IStateMachine
{
    private List<StateTransition<GameWideTrigger, S>> transitions;

    public void fireTrigger(GameWideTrigger trigger)
    {
        foreach (StateTransition<GameWideTrigger, S> transition in transitions)
        {
            if (transition.StartState == CurrentState)  // CS 0019 Operator 'operator' cannot be applied to operands of type 'S' and 'S'
        }
    }
}

谢谢canton7的正确回答:

== can mean a couple of different things (reference equality, a user-provided operator) and the compiler needs to figure out at compile-time which of those it is. It can't do that if it doesn't know anything about the type: S might be a value type with no == operator, and then what should happen? If you constrain S to be a reference type (where S : class) then the == will be allowed, as a reference comparison. Alternatively you can do EqualityComparer<T>.Default.Equals(x, y), which will call the type's Equals implementation