Allow mixing "init;" and "private set;" #7275
Replies: 3 comments 8 replies
-
The |
Beta Was this translation helpful? Give feedback.
-
Can you cite an example of how these classes would be used? |
Beta Was this translation helpful? Give feedback.
-
Since the following is already possible: public class Foo1
{
private string _value;
public required string Value
{
get => _value;
init => _value = value;
}
public void Update(string value) => _value = value;
} I'd question the value of this proposal. The occasions where you want public init, private set and provide a public method to work around that public init seem likely to be vanishingly rare. As @TahirAhmadov asks, do you have a real-world use case for where having a public |
Beta Was this translation helpful? Give feedback.
-
Sometimes we want our class public API to be immutable as possible and delegate all state management to the class itself.
The combination of
required
andinit
is very handy on this, but today it is not easy to have aninit
property withprivate set;
/protected set';
Would be nice to have a simple way to do this.
Some ideas:
When mixing
init
andset
should have a constraint that theset
must be less accessible thaninit
Beta Was this translation helpful? Give feedback.
All reactions