A little late to the party, but here's my implementation using implicit conversion to and from the ReadLimit class.
class ReadLimit<T> { private T _value; private readonly T _default; private int _maxReads; public ReadLimit(T value, int maxReads = 1, T defaultValue = default(T)) { _value = value; _default = defaultValue; _maxReads = maxReads; } public int Reads { get; private set; } public T Value => Reads++ == _maxReads ? (_value = _default) : _value; public override string ToString() => Value?.ToString(); public static implicit operator T(ReadLimit<T> limit) => limit.Value; public static implicit operator ReadLimit<T>(T value) => new ReadLimit<T>(value); }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A little late to the party, but here's my implementation using implicit conversion to and from the ReadLimit class.