Search code examples
c#genericsinterface

C# interface specfying a generic return type


I have something like:

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

Is this possible???


Solution

  • If the whole interface should be generic:

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

    If only the method needs to be generic:

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