0

Abstract Base Class

abstract class Base { public abstract void Method<T>(T args) { } } 

Derived Class several of these exist, each with a replacement for T

class Derived : Base { public override void Method<int>(int args) { } } 

Now, I know this isn't supported, but I need it because somewhere else in my code

class AnotherBaseClass<T, E> where E : Base { E e; // Will be actually one of the derived classes T t; // Simple types like string or int // I want this to work basically public void Func() { e.Method(t); } } 

this is going on.

What is the easiest solution to this? If I am able to do this, it will save me from writing a lot of code.

2 Answers 2

2

If you're fine with making Base a generic class, this is how:

abstract class Base<T> { public abstract void Method(T args); } class Derived : Base<int> { public override void Method(int args) {} } class AnotherBaseClass<T, E> where E : Base<T> { E e; T t; public void Func() { e.Method(t); } } 

If you need Base to not be generic, you can add this:

abstract class Base { public void Method<T>(T args) { var genericSelf = this as Base<T>; genericSelf.Method(args); } } 

and make Base<T> inherit Base and ensure your concrete classes always derive Base<T> and never Base directly.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, I forgot to write that I tried the first method (making Base a generic class), but it doesn't work when I want to use Base as a function argument for e.g. public void Fn(Base base). With the 2nd method I can use Base for function arguments and Base<T> for subclasses, right?
You must subclass Base<T> for concrete classes as I said, don't derive Base`` directly or else the this as Base<T>` will return null.
Yes I am using Base<T> for all concrete sealed classes and Base is just used for the function signature as I could not do public void Fn(Base<T> base). It gave compile errors.
you need to write public void Fn<T>(Base<T> base) for it to compile
0

I'm not exactly sure what problem you're sovling but under .Net 5 what you have in your question works as is...

abstract class Base { public abstract void Method<T>(T args); } class Derived : Base { public override void Method<T>(T args) { Console.Write($"{nameof(Derived)}.Method<{typeof(T)}>({args})"); } } class AnotherBaseClass<T, E> where E : Base { readonly E e; readonly T t; public AnotherBaseClass(T t, E e) { this.e = e; this.t = t; } public void Func() { e.Method(t); } } 

Run like so:

 static void Main(string[] args) { Derived d = new Derived(); var another = new AnotherBaseClass<int,Derived>(5, d); another.Func(); } 

Produces: "Derived.Method<System.Int32>(5)"

3 Comments

The question was about having a derived class with a non-generic method but rather a method implemented for a specific type, e.g. int
I am using .NET Framework 4.5, and yeah as @EldarS mentioned, this is not the solution I wanted
@EldarS this is literally what he said he was trying to do in his question. The code is not mine, it's his.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.