C# 泛型可以有特定的基类型吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/230736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:06:50  来源:igfitidea点击:

Can C# generics have a specific base type?

c#generics

提问by Neil C. Obremski

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 Tmust be a sub-class of BaseClass. Can that be done, are there plans for it, etc.?

显然上面的代码不起作用,但如果它起作用了,我想告诉编译器它T必须是BaseClass. 可以这样做吗,是否有计划等?

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.

我认为这在特定项目方面会很有用,确保在编译时不会将通用接口/类与非预期类型一起使用。或者也可以进行自我记录:显示打算使用哪种类型。

采纳答案by Ryan Lundy

public interface IGenericFace<T> where T : SomeBaseClass

回答by Ryan Lundy

yes.

是的。

public interface IGenericFace<T>
    where T : BaseClass
{
}

回答by Micah

What your are referring to is called "Generic Constraints". There are numerous constraints that can be put on a generic type.

您所指的称为“通用约束”。可以对泛型类型施加许多约束。

Some basic examples are as follows:

一些基本示例如下:

  • where T: struct- The type argument must be a value type. Any value type except Nullable- can be specified. See Using Nullable Types (C# Programming Guide)for more information.

  • where T : class- The type argument must be a reference type; this applies also to any class, interface, delegate, or array type.

  • where T : new()- The type argument must have a public parameterless constructor. When used together with other constraints, the new()constraint must be specified last.

  • where T : <base class name>- The type argument must be or derive from the specified base class.

  • where T : <interface name>- The type argument must be or implement the specified interface. Multiple interface constraints can be specified. The constraining interface can also be generic.

  • where T : U- The type argument supplied for Tmust be or derive from the argument supplied for U. This is called a naked type constraint.

  • where T: struct- 类型参数必须是值类型。Nullable可以指定除-之外的任何值类型。有关更多信息,请参阅使用可空类型(C# 编程指南)

  • where T : class- 类型参数必须是引用类型;这也适用于任何类、接口、委托或数组类型。

  • where T : new()- 类型参数必须有一个公共的无参数构造函数。当与其他约束一起使用时,new()必须最后指定约束。

  • where T : <base class name>- 类型参数必须是或派生自指定的基类。

  • where T : <interface name>- 类型参数必须是或实现指定的接口。可以指定多个接口约束。约束接口也可以是通用的。

  • where T : U- 为 提供的类型参数T必须是或派生自为 提供的参数U。这称为裸类型约束。

These can also be linked together like this:

这些也可以像这样链接在一起:

C#

C#

public class TestClass<T> where T : MyBaseClass, INotifyPropertyChanged, new() { }
public interface IGenericFace<T> where T : SomeBaseClass

VB

VB

Public Class TestClass(Of T As {MyBaseClass, INotifyPropertyChanged, New})
Public Interface IGenericInterface(Of T As SomeBaseClass)