Search code examples
c#constructorabstract-classtype-parameter

C# constructor for type parameterized abstract class


So I found a lot of answer to the question if and why it is ok to have a constructor defined in an abstract class.

I am currently trying to make a parameterized constructor available in an abstract class which has a type parameter:

public abstract class Cell<T>
{
    int address;
    T value;

    protected Cell<T>(int address, T value)
    {

    }
}

But c# simply refuses it and Intellisense completely breaks down. So why is it possible to have a constructor in an abstract class but as soon as the abstract class gets a type parameter everything refuses it?


Solution

  • Remove <T> from the constructor declaration and then everything will work. For example, this compiles just fine:

    public abstract class Cell<T>
    {
        int address;
        T value;
    
        protected Cell(int address, T value)
        {
    
        }
    }
    
    public class CellInt : Cell<int>
    {
        public CellInt(int address, int value): base(address, value) { }
    }