“where T : class, new()"是什么意思?意思是? [英] What does "where T : class, new()" mean?

查看:46
本文介绍了“where T : class, new()"是什么意思?意思是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能解释一下where T : class, new() 在下面这行代码中的含义吗?

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();

推荐答案

这是对泛型参数 T 的约束.它必须是一个 class(引用类型)并且必须有一个公共的无参数默认构造函数.

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.

这意味着 T 不能是 intfloatdoubleDateTime 或任何其他 struct(值类型).

That means T can't be an int, float, double, DateTime or any other struct (value type).

它可以是 string 或任何其他自定义引用类型,只要它具有默认或无参数构造函数即可.

It could be a string, or any other custom reference type, as long as it has a default or parameter-less constructor.

这篇关于“where T : class, new()"是什么意思?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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