2

I am trying to achieve something specific in c# and I don't know if it has a name (or even if it's good practice). I tried to simplify the code as much as possible because this is a generic problem (no pun intended, what I mean by that is that it doesn't depend on the rest of the implementation) Basically I have a generic class Class1, a class A, a class B that inherits from A, and another class Class2 that inherits from Class1 with B as the generic parameter. The issue is that I get an error "cannot convert from 'Class2' to 'Class1'" when I try to cast.

I tried different combinations and have spent hours and checked dozens of links but so far I can't seem to find an answer to this exact problem. Keep in mind I am fairly new to programming (a few years but I learned by myself)

public class Class1<T> where T : A
{
    public string name;
}

public class Class2 : Class1<B> { }

public class A { }
public class B : A { }

//if I change the parameter type to Class1<B>, but that defeats the purpose
//of being able to call this function with any class that extends Class1
void TestFunction(Class1<A> test)
{
    Debug.Log(test.name);
}

//I basically want to do this
var instance = new Class2();

//I can't do this because instance is of type Class2 but can't be converted
//to Class1<A>    
TestFunction(instance);

I want to be able to have classes that extend class1, like class2, with parameters that extend A, like B, but for a reason that I can't quite grasp it doesn't work this way. Thanks for any help! I'm kinda in over my head and feel like I will never find a solution that works at my level.

aybe
  • 15,516
  • 9
  • 57
  • 105
  • C# doesn't support multiple inheritance, but it does allow for multiple interfaces. Depending on what you want to do this might be an options for you. – pseudoku Apr 04 '19 at 23:24
  • Possible duplicate of [Multiple Inheritance in C#](https://stackoverflow.com/questions/178333/multiple-inheritance-in-c-sharp) – Steve Apr 04 '19 at 23:38
  • I guess the term "multiple inheritance" isn't the right one here, what I meant by it was more like "multi level inheritance" but I don't know what is the right word for it (basically a generic class inheriting from a generic class etc) – Crazy Chaze Apr 05 '19 at 08:28

1 Answers1

2

To get this one working you'll need to setup the function in a generic fashion providing everything the compiler needs to resolve it as A. Not exactly nice to have to restate the constraints but it works.

static void TestFunction<T>(Class1<T> test) where T : A
{

}
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
  • I found a better solution, if you think it's a bad one don't hesitate to tell me! I created an interface, and implementend it in class the child class `public interface ITest { }` `public class Class2 : Class1, ITest { }` I was wondering what the point of interfaces was, since I thought they were only here to "help" the developper by reminding him to implement the necessary methods, but now I see a different use. Edit : formatting – Crazy Chaze Apr 05 '19 at 08:51
  • @CrazyChaze - You've got to use what makes sense, sometimes an extra interface is the way too go. – ChaosPandion Apr 05 '19 at 11:24
  • Yeah, sometimes it's hard to know what the right solution is, and since I still have a lot to learn, sometimes it's even harder to know what is even possible. Anyway, thanks for the help! Have a good day – Crazy Chaze Apr 05 '19 at 11:30