I have a question about overriding properties in C#. There already is a similar question here, but the answers are not satisfying for me. Let's say I have these classes:
class A
{
public int Prop { get; }
}
class B : A
{
public int Prop { get; set; }
}
Now as you can see I want to add a setter to the property Prop in a subclass. I came up with two solutions. The first one is making the property virtual and overriding it in class B like this:
class A
{
public virtual int Prop { get; }
}
class B : A
{
public override int Prop { get; set; }
}
But unfortunately the compiler doesn't allow me to do this. My second idea was to use a 'new' keyword:
class A
{
public virtual int Prop { get; }
}
class B : A
{
public new int Prop { get; set; }
}
Now everything seemingly works, but it's not a satisfying solution for me because of one detail. Let's consider for instance this piece of code:
B b = new B();
b.Prop = 5;
A a = b;
Console.WriteLine(a.Prop);
You probably know that I get 0 here in my output, but I want to get 5. Is there any way to solve this problem?

