Zack
Zack

Reputation: 608

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!

Upvotes: 1

Views: 333

Answers (2)

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 262919

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)

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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)
    {
        ...
    }
}

Upvotes: 7

Related Questions