无法创建通用方法:找不到"T"

tut*_*ain 3 c# generics ado.net compiler-errors

我试图实现一个返回通用列表(List)的方法,但我不断收到此错误消息:

找不到类型或命名空间名称"T"(您是否缺少using指令或程序集引用?)

这是我的方法代码:

public static List<T> doQuery(string query)
{
    SQLiteCommand com = new SQLiteCommand(query, SQLiteManager.connection);
    SQLiteDataReader reader = com.ExecuteReader(CommandBehavior.Default);
    while (reader.Read())
    {
        //other code
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么在这种情况下T不被识别为通用类型?

Thu*_*wat 5

你需要告诉方法什么是"T",现在你的方法不知道T是什么.T在编译时已知,语言不能当场计算出类型.

这是一个例子: static List<T> GetInitializedList<T>(T value, int count)

参考这里:http://www.dotnetperls.com/generic-method

  • 只是想在Generics上添加MSDN文档,因为它与这个问题/答案有关.https://msdn.microsoft.com/en-us/library/512aeb7t.aspx (2认同)