There is a task:

public class A { } public class A1 : A { } public class A2 : A { } public class B<T> where T : A { } public class B1 : B<A1> { } public class B2 : B<A2> { } public class Conteiner<T, U> where T : B<U> where U : A { T[] list; public T this[int index] { get { return list[index]; } set { list[index] = value; } } public void Set<K, N, M>(K conteiner) where K : Conteiner<N, M> where N : B<M> where M : A { list[0] = conteiner[0]; <------Ошибка не удается преобразовать тип N в T. } } 

How to solve or work around a problem?

  • Override Set : public void Set(Conteiner<T, U> conteiner) . Otherwise, it is simply impossible, since you can have U=A1 , and M=A2 and these types are not given - Andrey NOP
  • one
  • What is the point of doing Conteiner<T, U> and then writing there Conteiner<N, M> . What problem are you trying to solve? - tym32167
  • I needed to create classes (Conteiner and his descendants) storing lists of classes inherited from A. It is necessary because each heir stores certain data, and containers are needed for their entry and withdrawal. This code is necessary in order to quickly convert the data. - Abdullah

1 answer 1

You can do by doing so:

 public class Conteiner<T, U> where T : B<U> where U : A { T[] list; public T this[int index] { get { return list[index]; } set { list[index] = value; } } public void Set<K,N,M>(Conteiner<T, U> conteiner) where K : Conteiner<N, M> where N : B<M> where M : A { list[0] = conteiner[0]; } } 
  • Then <K,N,M> not needed, are they? - Andrey NOP