简体   繁体   English

通用列表方法问题

[英]Generic List Method Problem

I'm getting an error when I try to create a method with the following signature: 尝试创建具有以下签名的方法时出现错误:

public List<T> CreateList(DataSet dataset)


Error 1 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

Does anyone know what I'm doing wrong? 有人知道我在做什么错吗?

Thanks in advance! 提前致谢!

T must be declared either at the method level: T必须在方法级别声明:

public List<T> CreateList<T>(DataSet dataset)

or at the containing class level: 或在包含的类级别:

public class Foo<T>
{
    public List<T> CreateList(DataSet dataset)
    {
        ...
    }
}

But be careful to not declare it at both places: 但是请注意不要在两个地方都声明它:

// Don't do this
public class Foo<T>
{
    public List<T> CreateList<T>(DataSet dataset)
    {
        ...
    }
}

Since you're defining a generic method, the type placeholder should be part of the method declaration, not only of its return type. 由于您正在定义通用方法,因此类型占位符应该是方法声明的一部分,而不仅是其返回类型。 Try: 尝试:

public List<T> CreateList<T>(DataSet dataset)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM