开发者

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开发者_C百科?

Thanks in advance!


T must be declared either at the method level:

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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜