1

I have following class structure:

public class X
{
        int foo = 0;
}

public class Y : X
{
        int bar = 0;
}

public class Z: X
{
        int foo2 = 0;
}

public abstract class A<T> where T : X
{
         public abstract List<T> {get; set;}
}


public class B: A<Y>{}

public class C: A<Z>{}

Now I've the problem that I want to use these objects in a worker class like this:

public class Worker
{
        B b = new B();
        C c = new C();

        public static void main()
        {
        doSome(b);
        doSome(c);
        }

        private static void doSome(A<X> a)
        {
        }
} 

Both derived types should be processed in this Method (it only needs the base properties). But I get the Error "Cannot convert from B to A < X >"

Is there any chance to achive something like this?

Florian
  • 21
  • 5
  • 1
    only because `Y` derives from `X`, doesn´t mean `A` also derives from `A`. In fact both types are completely different and don´t have anything in common. If that **would** work, you could also do this: `A a = (A) myB; a.List.Add(new C());`, so your list would contain both `B` and `C`-instances. – MakePeaceGreatAgain Feb 23 '21 at 09:51

0 Answers0