&是什么QUOT;其中T:类,新的()"意思? [英] What does "where T : class, new()" mean?
本文介绍了&是什么QUOT;其中T:类,新的()"意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
能否请您给我什么解释其中T:类,新的()
表示在下面的代码行?
无效添加< T>(T项目),其中T:类,新();
解决方案
这是对泛型参数约束 T
。它必须是一个类
(引用类型),而且必须有一个公共的无参数的默认构造函数。
这意味着 T
不能是 INT
,浮动
,双击
,的DateTime
或任何其他结构
(价值型)。
这可能是一个字符串
,或任何其他自定义引用类型,只要它有一个默认或参数-less构造。
Can you please explain to me what where T : class, new()
means in the following line of code?
void Add<T>(T item) where T : class, new();
解决方案
That is a constraint on the generic parameter T
. It must be a class
(reference type) and must have a public parameter-less default constructor.
That means T
can't be an int
, float
, double
, DateTime
or any other struct
(value type).
It could be a string
, or any other custom reference type, as long as it has an default or parameter-less constructor.
这篇关于&是什么QUOT;其中T:类,新的()"意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文