Search code examples
c#genericsinterfaceconstraints

C# Generics constraint with Interface to a method


probably the title is not right but i can't figure out something more precise.

I have this class:


    Transport<T> where T: ISomething

I have another class where i can have my "Transport" class with his generics that implement my ISomething. Like This:


    public class Route 
    {
        Transport<ISomething> Transport;
        public Route(Transport<ISomething> t)
        {
            Transport = t;
        }
    }

I want to be able to call my constructor


    Transport<Potatoes> myTransport = GetAllPotatoes();
    
    Route myRoute = new Route(myTransport);

Is there a way to do this? I'm new to generics and (as non native english speaker) i can't use the right keywords to find the answer myself.

Thanks.

EDIT for clarity: Potatoes implements ISomething.


Solution

  • You can make it with a covariant interface... in your case:

    public interface ISomething {
    }
    
    public interface ITransport<out T> where T : ISomething
    {
    }
    
    public class Transport<T> : ITransport<T> where T: ISomething
    {
    }
    
    public class Potatoes : ISomething {
    }
    
    public class Route 
    {
      ITransport<ISomething> Transport;
      public Route(ITransport<ISomething> t)
      {
        Transport = t;
       }
    }
    
    public class Program
    {
        public static void Main()
        {
            Transport<Potatoes> myTransport = null /* or getAllPotatoes */;   
            Route myRoute = new Route(myTransport);
        }
    }
    

    Note that Route now takes an ITransport<T>, not a Transport<T>.

    Covariance in classes doesn't work, you need an interface for that.

    This doesn't do anything, but just so you see it compiles: https://dotnetfiddle.net/T2Yd8N