C#状态<t>中的泛型

时间:2016-03-18 09:14:02

标签: generics

我创建了一个类:

public class State<T>
{
    private T state; 
    private double cost;    
    private State<T> cameFrom; 
    public State(T state)    // CTOR
    {
        this.state = state;
        cost = 0;
        cameFrom = null;
    }

然后我打开了一个界面:

public interface ISearchable
{
    State<T> getInitialState();
    State<T> getGoalState();
    List<State<T>> getAllPossibleStates(State<T> s);
}

它在界面上标记我出现以下错误: 类型或命名空间名称&#39; T&#39;找不到(你错过了使用指令或汇编引用吗?)

为什么呢?

我该如何解决?

1 个答案:

答案 0 :(得分:1)

编译器不知道如何解析接口方法中的类型T。您还需要使接口或方法通用:

public interface ISearchable<T>

或者:

public interface ISearchable
{
    State<T> GetInitialState<T>();
    State<T> GetGoalState<T>();
    List<State<T>> GetAllPossibleStates<T>(State<T> s);
}

顺便说一下,C#中的通常惯例是使用方法名称的初始大写。