指定通用返回类型的 C# 接口(interface)

标签 c# generics interface

我有这样的东西:

public interface IExample
{
  int GetInteger()
  T GetAnything(); //How do I define a function with a generic return type???
^^^^^
}

这可能吗???

最佳答案

如果整个界面应该是通用的:

public interface IExample<T>
{
  int GetInteger();
  T GetAnything();
}

如果只是方法需要泛型:

public interface IExample
{
  int GetInteger();
  T GetAnything<T>();
}

关于指定通用返回类型的 C# 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10147292/

相关文章:

delphi - Spring4D中如何使用多接口(interface)类

go - 无法根据 Golang 中的条件将接口(interface)转换为结构

c# - 如何在不实际执行查询的情况下测试查询数据库的方法?

c# - 当数据库中的数据发生变化时更新变量

c# - 泛型阻止虚函数调用?

generics - 如何在 F# 中使用通用计量单位指定联合子句?

arrays - 系统Verilog : How to create an interface which is an array of a simpler interfaces?

c# - LINQ to Entities 无法识别方法 'System.Linq.IQueryable

c# - 将 "option(maxrecursion 0)"与 Entity Framework Entity Sql 一起使用

java - Java 迭代器返回的结果的泛型类型不兼容