Skip to content

Allow child classes to add a setter for covariant overridden properties #5179

Discussion options

You must be logged in to vote

If you change the definition of Teacher to this:

class Teacher : Person {
  public override Organization Employer { get; set; }
}

then you still get a compiler error, albeit a different one:

error CS0546: 'Teacher.Employer.set': cannot override because 'Person.Employer' does not have an overridable set accessor

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.

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@spydacarnage
Comment options

@JustNrik
Comment options

@jnm2
Comment options

@ethanresnick
Comment options

@theunrepentantgeek
Comment options

Answer selected by ethanresnick
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants