Allow child classes to add a setter for covariant overridden properties #5179
-
I.e., I'm proposing that things like this should be allowed: class Person {
public virtual Organization Employer { get; }
}
class Teacher : Person {
public override School Employer { get; set; } // child class cannot add `set` modifier here
}
class Nurse : Person {
public override Hospital Employer { get; set; } // ditto
} I understand why |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
If you change the definition of class Teacher : Person {
public override Organization Employer { get; set; }
} then you still get a compiler error, albeit a different one:
So, while the original error message may not be ideal, it is certainly consistent. If the language was modified to allow a set accessor to be added in the covariant case, it would also need to be modified to allow it in the non-variant case. I strongly suspect there are very good reasons why this isn't currently supported, starting with the potential to violate Liskov substitution. |
Beta Was this translation helpful? Give feedback.
If you change the definition of
Teacher
to this:then you still get a compiler error, albeit a different one:
So, while the original error message may not be ideal, it is certainly consistent.
If the language was modified to allow a set accessor to be added in the covariant case, it would also need to be modified to allow it in the non-variant case.
I strongly suspect there are very good reasons why this isn't currently supported, starting with the potential to violate Liskov substitution.