C#泛型能有一个特定的基类型吗? [英] Can C# generics have a specific base type?

查看:104
本文介绍了C#泛型能有一个特定的基类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

泛型接口的类型是否可能基于特定的父类?



例如:

  public interface IGenericFace< T:BaseClass> 
{
}

显然上面的代码不起作用,但如果它做了什么,我试图告诉编译器: T 必须是 BaseClass 的子类。可以这样做,是否有计划等等?



我认为这对于一个特定的项目是有用的,确保一个通用的接口/类不是在编译时没有使用非预期的类型。或者也可以对自我文档进行排序:显示哪种类型是有意的。

解决方案 > public interface IGenericFace< T> T:SomeBaseClass


Is it possible for a generic interface's type to be based on a specific parent class?

For example:

public interface IGenericFace<T : BaseClass>
{
}

Obviously the above code doesn't work but if it did, what I'm trying to tell the compiler is that T must be a sub-class of BaseClass. Can that be done, are there plans for it, etc.?

I think it would be useful in terms of a specific project, making sure a generic interface/class isn't used with unintended type(s) at compile time. Or also to sort of self-document: show what kind of type is intended.

解决方案

public interface IGenericFace<T> where T : SomeBaseClass

这篇关于C#泛型能有一个特定的基类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆