I am currently looking at a class that is created this way:
public class MyClass<T> : MyClass<T, OtherInfo>
OtherInfo
is an existing type.
To me, it looks like this class is inheriting from itself. Is this true?
CodePudding user response:
You can write methods in the same class that have the same name, but you have to make sure that they have different parameters. For example, this is valid:
void Foo() {}
void Foo(string p1) {}
void Foo(int p2) {}
void Foo(string p1, int p2) {}
This means to correctly identity which method to execute you need to know both the name and its parameters.
You can do the same thing with types. You can have multiple types with the same name, but different generic arguments. This is also valid:
public class MyClass : MyClass<int> {}
public class MyClass<T1> : MyClass<int, int> {}
public class MyClass<T1, T2> {}
Just like with methods, to identify which class you're referring to you need both its name and generic arguments.
In your case, MyClass<X>
is a different class than MyClass<X,Y>
- they are defined independently in your source code (or referencing library). So no, it's not inheriting from itself.