C# 在不指定泛型类的情况下实现泛型接口
我想创建泛型接口,但在实现此接口时不指定泛型类C# 在不指定泛型类的情况下实现泛型接口,c#,generics,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(); }
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();
}
}
公共接口信息系统
{
T这个[int id]{get;}
}
公共课学生:我是//我不想在这门课上留级
{
公立学生本[int id]
{
get=>newstudent();
}
}
不能忽略显式类型参数;C#不会让你的。必须为泛型超类型中出现的每个类型变量指定一个类型参数
类型变量(也称为泛型参数)是在实例化或实现泛型类型时指定的其他类型的占位符。编译器不会为您推断类型参数,至少不会在类型声明中推断。为什么您认为可以省略它?任何人怎么知道你关心的是哪一个泛型参数?你想要的被称为,但没有编程语言实现它们。在这种情况下,您总是必须重复类名。