Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generic method in abstract class

I have the following abstract base class in which i have an abstract method. I need to know how to implement this abstract method in the child classes. The problem is how do I declare a class whose base is SomeBaseClass in Class B.

public abstract class A
{
    protected abstract void Add<T>(T number) where T : SomeBaseClass;
}

public class B : A
{
    protected override void Add<T>(T number)
    {
        throw new NotImplementedException();
    }
}
like image 461
stech Avatar asked Apr 13 '12 16:04

stech


1 Answers

I think you want the base class to have a type parameter, not a specific method:

public abstract class A<T> where T : SomeBaseClass
{
    protected abstract void Add(T number);
}

public class B : A<C> {

    protected void Add(C number) { ... }
}
like image 139
Chris Shain Avatar answered Oct 18 '22 05:10

Chris Shain