I am still trying to fully understand generics especially when used in conjunction with base classes. I have a generic table class that takes in a derived type of a row class but is constrained to be a base row. Since the base table class is constraint to taking a type base row how can I pass this class with any derived class to be of base row type.
public interface Itable<in TRow> where TRow : row{
//*This will not compile with the list of TRow should it be List<row>? If so how do i force that conversion?
List<TRow> IfindLst();
}
public abstract class table<TRow> where TRow : row, Itable<row>
{
//*This is the list of rows i want to be able to send as their base row
public List<TRow> derivedRowLst = new List<TRow>();
public table()
{
}
public List<row> IfindLst()
{
return derivedRowLst;
}
}
//Derive instance of base table class
public class tableInstOne : table<rowInstOne>
{
}
//Base class for row that all derived rows are guarantee to be of
public abstract class row
{
}
public class rowInstOne : row
{
public rowInstOne() { }
}
public class tester
{
public static void main()
{
Itable<row> tblInstOne = new tableInstOne();
//*This is what I am trying to figure out how to do get this list of base row class from the base table class. Even though it is stored as List<TRow> or derived row class.
List<row> baseLst = tblInstOne.IfindLst();
}
}
This does not allow me to send the instantiated table class as having its base guaranteed type. I used to not have the table class as generic and so all rows were just used as base row but this required downcasting at other points in code that I am trying to avoid. Now I don't have to downcast BUT I cannot send this table class with base as a parameter to classes that don't care about the derived row type and only need to utilize the base row functions. Thanks for any help!