为什么会有自属类型约束?

时间:2018-07-25 09:20:31

标签: c# inheritance generic-constraints

我正在查看一些前雇员编写的代码,并发现了

public abstract class BaseClass<T, M> 
where T : BaseClass<T, M>, M
where M : IDisposable
{
    protected M PropertyName {get; private set}
    ...
    // T is never used in this class. Only M is used
}

继承的类是这样定义的

public class InheritedClass : BaseClass<InhertiedClass, IFooInterface>, IFooInterface
{
   ...
}

IFooInterface : IDisposable {...}

我的问题是,为什么基类指定一个从自身继承的泛型T?如果删除此通用约束,那么什么都不会改变,所以我想知道为什么它首先出现在这里?指定我不知道的通用约束有什么好处?

0 个答案:

没有答案