声明泛型类时new()的目的是什么? [英] What is the purpose of new() while declaration of a generic class?

查看:214
本文介绍了声明泛型类时new()的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在声明BaseEntityCollection类时new()的目的是什么?

如果要删除它,则会出现以下错误消息: T必须是非抽象类型带有公共无参数构造函数,以便将其用作参数...

What is the purpose of new() while declaration of BaseEntityCollection class?
If I'm going to remove it, I got an error with the following message "T must be a non-abstract type with a public parameterless constructor in order to use it as parameter ..."

public abstract partial class BaseEntityCollection<T> : 
       List<T> where T : BaseEntity, new()


推荐答案

无论您为 T 指定什么类,它都有一个默认的(无参数)构造函数。

It means that whatever class you specify for T, it has a default (no parameters) constructor.

因此,在泛型中类,您可以执行 new T(),它将创建一个T类型的新对象。

Therefore, in the generic class, you can do new T() and it will create a new object of type T.

这篇关于声明泛型类时new()的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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