实现通用接口而不指定泛型类

时间:2018-03-14 11:48:48

标签: c# generics

我想创建通用接口,并且在实现此接口时不要指定泛型类

public interface IThis<T>
{
    T this[int id] { get; }
}

public class Student : IThis<Student> // i dont want repeat Student in this
{
    public Student this[int id]
    {
        get => new Student();
    }
}

1 个答案:

答案 0 :(得分:0)

您不能省略显式类型参数; C#不会让你。 必须为通用超类型中出现的每个类型变量指定一个类型参数。

类型变量(也称为泛型参数)是在实例化或实现泛型类型时将指定的其他类型的占位符。编译器不会为您推断类型参数 - 至少不会在类型声明中。

相关问题