Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract property with type declared in derived class?

Is it possible to have an abstract property which returns a type defined in the derived class:

abstract class baseClass
{
    public abstract e_Type type { get; }
}

class derived : baseClass
{
    public enum e_Type
    {
        type1,
        type2
    }

    private e_Type _type;
    public e_Type type { get { return _type; } }
}

or must I return an int and map it in the derived class. Any other suggestions welcome.

like image 984
Patrick Avatar asked Dec 21 '22 20:12

Patrick


1 Answers

Sure you can:

abstract class BaseClass<T>
{
    public abstract T Type { get; }
}

class Derived : BaseClass<EType>
{    
    public enum EType
    {
        type1,
        type2
    }

    private EType _type;
    public override EType Type { get { return _type; } }
}

You don't even need to declare it as abstract:

class BaseClass<T> {
    private T _type;
    public T Type { get { return _type; } }
}

which you can then use as:

BaseClass<EType> classInst = new BaseClass<EType>();
like image 169
thecoop Avatar answered Jan 13 '23 11:01

thecoop