关键字"where"在哪里?在类声明中做什么? [英] What does the keyword "where" in a class declaration do?

查看:28
本文介绍了关键字"where"在哪里?在类声明中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看MvcContrib网格的源代码,并看到声明为的类:

I'm looking at the source code for the MvcContrib Grid and see the class declared as:

public class Grid<T> : IGrid<T> where T : class

其中的T:class 位有什么作用?

推荐答案

它是通用类型约束.

在这种情况下,这意味着通用类型( T )必须是引用类型,即类,接口,委托或数组类型.

In this case it means that the generic type (T) must be a reference type, that is class, interface, delegate, or array type.

此处列出了其他限制.

您还可以限制泛型从特定类型(基类或接口)继承

You can also constrain the generic type to inherit from a specific type (base class or interface)

这篇关于关键字"where"在哪里?在类声明中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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