c# – Generic List Method Problem – Education Career Blog

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:

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)

Leave a Comment