2010-10-20 61 views
1

我得到一個錯誤,當我嘗試創建一個方法具有以下簽名:泛型列表的方法問題

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

有誰知道我做錯了嗎?

提前致謝!

+0

感謝您的所有答案,幫我加載! – Zack 2010-10-20 09:18:58

回答

7

T必須聲明爲在方法級別:

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

或含類級別:

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

但要注意在這兩個地方不是聲明它:

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

既然你是定義通用方法中,類型佔位符應該是方法聲明的一部分,而不僅僅是它的返回類型。試試:

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