什么是一个通用类默认构造函数的语法? [英] What is the syntax for a default constructor for a generic class?

查看:72
本文介绍了什么是一个通用类默认构造函数的语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时在C#中禁止实施泛型类默认构造函数?

Is it forbidden in C# to implement a default constructor for a generic class?

如果不是,为什么下面的代码不编译? (当我删除< T> 它编译虽然)

If not, why the code below does not compile? (When I remove <T> it compiles though)

什么是定义一个默认的构造函数的正确方法吗?那么泛型类

What is the correct way of defining a default constructor for a generic class then?

public class Cell<T> 
{
    public Cell<T>()
    {
    }
}

编译时错误:错误1无效令牌(类,结构或接口成员声明

Compile Time Error: Error 1 Invalid token '(' in class, struct, or interface member declaration

推荐答案

您没有提供构造函数中的参数类型,这是你应该怎么做。

You don't provide the type parameter in the constructor. This is how you should do it.

public class Cell<T> 
{
    public Cell()
    {
    }
}

这篇关于什么是一个通用类默认构造函数的语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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