Harlan said:
To make autoproperties even more flexible, I'm wondering about the
prospect of readonly functionality being made available. Are there
reasons why it would be unwise, impractical, or harmful to allow
public readonly MyProperty { get; private set; }
or
public MyProperty { get; private readonly set; }
meaning that the setter can only be called from inside a constructor?
The main issue is, as Patrice says, the question of balancing need with
cost.
Every language change comes with a cost, sometimes a fairly large one
(even for what appears to be a simple change). Automatic properties are
a convenience, not necessary for writing full-featured C# programs, and
the more complicated they get, the worse the cost:benefit ratio gets.
Now, that said, I think it's possible that this specific feature -- some
form of read-only behavior on automatic properties -- may actually
appear some time. There's no fundamental reason it wouldn't work, and
there's enough of a benefit to read-only semantics that it could be
viewed as beneficial enough to justify _some_ degree of complication to
the automatic property syntax.
One should not expect a large change. The more variation in behavior
that's possible with an automatic property, the more likely it is that
the C# programmer should just be using a non-automatic property.
On a related note: one suggestion I've seen is to allow a property to
have its own members, so that implementation details for the property
are fully encapsulated. This doesn't address the automatic property
issue, but it turns out that at least some people who are asking for
better read-only support in automatic properties, they are _really_
looking for a way to continue to take advantage of the implicit
encapsulation hiding of automatic properties (i.e. the fact that the
backing field is inaccessible to any other code), but to at the same
time have a specific read-only behavior.
Providing that encapsulation via other means than automatic properties
(after all, again...automatic properties are really just a convenience,
rather than being intended as a way to provide additional OOP
functionality) could be viewed as more worthwhile than meddling with the
automatic property design, especially since it would provide real
benefits to a broader range of C# users.
All that said, this kind of stuff is all up to the C# language design
team. They have a much better "big picture" view, and while probably a
feature along these lines is probably something they'd consider, I'm
sure they also have a long list of other stuff they'd also like to
include. At the same time, there's value in the language evolving
slowly and carefully. So any one "pet feature" one might want has a
relatively low chance of being included, even if it's something that
would be really useful.
Pete