Allow init and set accessor in property #4164
-
Hi, I've came across a situation where I wanted a property to have an init accessor and a private set accessor, to init it (without a constructor) and also allow to modify it from this class. private sealed class WaitAction : Action
{
public int Ticks { get; init; private set; }
public override bool MoveNext(Entity entity)
=> --Ticks > 0;
} Then when I wanted to initialize it : Of course, I could write a constructor and make the prop { get; private set; }, or remove the init and make the prop { get; set; } |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I don't think that this would be possible as the private sealed class WaitAction : Action
{
private int ticks;
public int Ticks {
get => ticks;
init => ticks = value;
}
public override bool MoveNext(Entity entity)
=> --ticks > 0;
} |
Beta Was this translation helpful? Give feedback.
I don't think that this would be possible as the
init
accessor already occupies the slot in the runtime for aset
accessor. What you could do instead is to explicitly define your backing field and update it directly: