C#C
C#4y ago
Klarth

Nullable approach with property defined in base and initialized in derived classes [Answered]

Is there an approach for the Base.Some property besides changing it to SomeType? or suppressing with = null!;. I want to defer the creation and leave it up to a derived class.

public abstract class Base
{
    public SomeType Some { get; set; }
}

public class DerivedA : Base
{
    public DerivedA()
    {
        Some = new SomeType(1, 2, 3);
    }
}

public class DerivedB : Base
{
    public DerivedB()
    {
        Some = new SomeType(9, 8, 7);
    }
}

public class SomeType
{
    public SomeType(int a, int b, int c) { }
}
Was this page helpful?