-1

I have a an abstract base class FooBase that's inherited by Foo. And I have another abstract base class BarBase that's inherited by Bar. BarBase contains an abstract FooBase property. And in Bar I want to set the FooBase property to Foo. Why is this not allowed? Is this a crazy design that needs to be rethought, or can it be done in some way?

public abstract class FooBase
{
}

public class Foo : FooBase
{
}

public abstract class BarBase
{
    public abstract FooBase Foo { get; set; }
}

public class Bar : BarBase
{
    public override Foo Foo { get; set; }
}
Oystein
  • 1,232
  • 11
  • 26

1 Answers1

1

Because you can't change the signature when overriding. Period. That is how C# was designed. A better solution here would be using generics:

public abstract class FooBase
{
}

public class Foo : FooBase
{
}

public abstract class BarBase<F> where F : FooBase
{
    public F Foo { get; set; }
}

public class Bar : BarBase<Foo>
{
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325