What does the keyword “where” in a class declarati

2020-02-10 17:34发布

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

What does the where T : class bit do?

标签: c#
7条回答
趁早两清
2楼-- · 2020-02-10 18:14

Another examples would be

public A<T> where T : AnInterface

where AnInterface is a interface class. It means then, that T must implement this interface.

These constraints are important, so that the compiler knows the operations which are valid for the type. For example you can not call functions of T without telling the compiler what functions the type provides.

查看更多
登录 后发表回答