简体   繁体   English

为什么我不能声明通用列表 <T> 作为接口方法的返回类型?

[英]Why can't I declare generic List<T> as return type for interface method?

Why can't I create an interface method with return type List<T> ? 为什么我不能创建返回类型为List<T>的接口方法? I'm encountering this compile error "The type or namespace name 'T' could not be found" 我遇到此编译错误“找不到类型或名称空间名称'T'”

interface ILoader 
{        
    List<T> LoadValues();
}

Appreciate any of your input. 感谢您的任何投入。

What is T supposed to be in your interface? T应该在您的界面中是什么? For the compiler, T is just an unknown identifier at that point. 对于编译器, T在那时只是一个未知的标识符。

You need to specify something for T , some type that is used as the type argument for List<T> . 您需要为T指定一些内容,将某些类型用作List<T>的类型参数。


Depending on what you want to achieve, there are four options: 根据您要实现的目标,有四个选项:

  • Declare T as a generic parameter on the interface/class level: 在接口/类级别上将T声明为通用参数:

     interface ILoader<T> { List<T> LoadValues(); } 
  • Declare T as a generic parameter on the method level: T声明为方法级别的通用参数:

     interface ILoader { List<T> LoadValues<T>(); } 
  • Assign a fixed type as T ; 将固定类型分配为T eg int (wlog): 例如int (wlog):

     interface ILoader { List<int> LoadValues(); } 
  • Declare T as a generic parameter on a higher nesting level (thanks to Jeppe Stig Nielsen for pointing out this possibility): 在较高的嵌套级别上将T声明为通用参数(感谢Jeppe Stig Nielsen指出了这种可能性):

     class Outer<T> { interface ILoader { List<T> LoadValues(); } } 

The T generic parameter must also be declared in the interface. T泛型参数也必须在接口中声明。 Something like this: 像这样:

interface ILoader<T>
{        
    List<T> LoadValues();
}

You should do something like this: 您应该执行以下操作:

interface ILoader<T>
{
    List<T> LoadValues();
}

Or 要么

interface ILoader
{        
    List<T> LoadValues<T>();
}        

Or even, possibly: 甚至可能:

interface ILoader 
{        
    List<dynamic> LoadValues();
}

^Isn't the best idea but usable. ^不是最好的主意,但可以使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么方法不能声明返回类型`List <IMyInterface>`并返回实现它的具体类 - Why can't a method declare return type `List<IMyInterface>` and return a concrete class implementing it 我可以声明一个接口方法来返回C#中的通用接口吗? - Can I declare an interface method to return generic Interface in C#? 为什么我不能在接口中声明公共方法? - Why can't I declare a public method in an interface? 除非T实现接口I,否则需要通用方法返回类型T,在这种情况下,返回I - Need generic method to return type T unless T implements interface I, in which case return I 为什么我不能将&#39;as&#39;用于限制为接口的泛型类型参数? - Why can't I use 'as' with generic type parameter that is constrained to be an interface? 为什么我不能使用与继承接口相同的泛型类型? - Why can't I use the same generic type with inherited interface? 我如何返回通用列表 <T> 从通用方法 <T> () - How can i return a generic List<T> from a generic method<T>() 为什么我不能将一个项目转换为泛型类型——其中泛型类型是一个接口,并且在检查该项目是否实现了所述接口之后? - Why can't I cast an item to a generic type — where the generic type is an interface, and after checking that the item implements said interface? 为什么我不能在 C# 中将匿名类型作为泛型返回,而我可以对方法参数执行相同的操作? - why I can't return anonymous type as generic in C#, while I can do the same for method parameters? 为什么我不能用返回具体类型的方法实现返回接口的接口方法 - Why can't I implement an interface method that returns an interface with a method that returns a concrete type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM