You can't. Assignment overwrites the original variable; it doesn't allow you to just overwrite one property of the variable and leave the rest alone. Also, struct
's are supposed to be immutable, so modifying just the Value
property, in any way, shouldn't be allowed. If you really want to allow this, I'd recommend using a class
instead of a struct
, and making the Value
setter public.
However, if you want to keep it as a struct
, I'd recommend something like this instead:
public Field<T> WithValue(T value)
{
return new Field<T>(this.Ordinal, this.Number, value);
}
And then call it like this:
dateOfBirth = dateOfBirth.WithValue(DateTime.Today);
Also note, I'd strongly recommend making the operator T
explicit, because it discards the Ordinal
and Numeric
values. See this note from MSDN:
If a conversion operation can cause exceptions or lose information, you should mark it
explicit
.