I want to put a non generic class into a List with the type of a generic base class. However the code wont combile with the error that a conversion is not possible. I am pretty new to C# so I might just miss something very basic here or my approch to use generics that way is just no good idea? My code looks something like this:
public abstract class AbstractClass{
public string foo;
}
public class A : AbstractClass{
public int bar
}
public class B : AbstractClass{
public int baz
}
public abstract class AbstractBehaviour<T> : MonoBehaviour where T : AbstractClass{
public T data;
public abstract string compute(T arg);
}
public class ABehaviour : AbstractBehaviour<A>{
//some implementation of compute
}
public class BBehaviour : AbstractBehaviour<B>{
//some implementation of compute
}
With this setup I would like to do something like this:
ABehaviour a = new ABehaviour();
BBehaviour b = new BBehaviour();
List<AbstractBehaviour<AbstractClass>> list = new List<AbstractBehaviour<AbstractClass>>();
list.Add(a);
list.Add(b);
Thank you very much.