I'm having an interface IJob from which all Job classes inherits and I have generic repo class which will process all kind of IJob's.
The problem im facing is im not able to able convert Repository<Job1>
to Repository<IJob>
even though Job1
is a type of IJob
.
Code
internal class Program
{
public interface IJob { }
public class Job1 : IJob { }
public class Job2 : IJob { }
public class Repository<TJob>
where TJob : IJob
{
public List<TJob> GetJobs()
{
return new List<TJob>();
}
}
private static void Main(string[] args)
{
IJob iJob = new Job1(); // Valid
Repository<IJob> repo = new Repository<Job1>(); // Not Valid
}
}
Can someone let me know why this wont be possible in C# and are there any other rules type conversion in C# generics?