Is there a way to have a private readonly field in a class that could be assigned a value anywhere in the class, but only once??
That is, I am looking for a private readonly kind of field which could be assigned a value only once, but not necessarily inside the constructor. So that, if a value is re-assigned to a field then it shows compile-time error ( I am sure that is asking for too much).
If there is any pattern (not language feature) that could do the same job, would really be interested in knowing that too.
Thanks for your interest.
The short answer is no. The only place you can have a one-time assignment that is compiler checked is in the constructor. You could create a system where you’d get a run-time error if an assignment was attempted more than once but there is no C# construct to do this