With only a bit of twitches you can get a much more flexible design:
publicclassReadValueOnlyXTimesThingy<T>{privateT_value;publicR1V(intmaxNumberOfReads,Tvalue){if(maxNumberOfReads<=0)thrownewArgumentOutOfRangeException("maxNumberOfReads must be a positive value");ReadsLeft=maxNumberOfReads;_value=value;}publicintReadsLeft{get;privateset;}publicTValue{get{if(ReadsLeft<=0){_value=default(T);}ReadsLeft--;return_value;}}}
And the usage:
varselfDestructingMessage=newReadValueOnlyXTimesThingy<string>(3,"this message will self destruct after 3 reads.");// some code herevartheMessage=selfDestructingMessage.Value;
Notes:
This allows as many reads as set in it's constructor.
In case T is a value type, it's default will return a value - 0 for numbers, false for bool and so on.
This might not be a desirable outcome once the value have been read the allowed number of times, since it's default value might be indistinguishable from the previous value (think of initializing this class with 1, false - you'll always get false back.
One way to handle this is to restrict the T to reference types only, another is to change the property return type to a tuple of bool, T - or perhaps throw an exception - Though that would probably be the last thing I would recommend - as this kind of thing is what Eric Lippert would call a vexing exception, and as usual, he knows what he is writing about.
Programmed Canon Canola calculators in 1977. Assorted platforms and languages ever since. Assisting with HOPL.info.
I am NOT looking for work -- I've got more than enough to do.
Location
Perth, WA Australia
Education
A few diplomas.
Work
Software Engineer at [Daisy Digital](https://daisydigital.com.au/)
If you are already using this nuget, then yes. Otherwise, assuming you're only working with reference types, It introduces more complexity than a simple is object test for reference types.
Anyway, this is a good option to keep in mind.
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.
With only a bit of twitches you can get a much more flexible design:
And the usage:
Notes:
default
will return a value -0
for numbers,false
for bool and so on. This might not be a desirable outcome once the value have been read the allowed number of times, since it's default value might be indistinguishable from the previous value (think of initializing this class with 1, false - you'll always getfalse
back. One way to handle this is to restrict theT
to reference types only, another is to change the property return type to a tuple ofbool, T
- or perhaps throw an exception - Though that would probably be the last thing I would recommend - as this kind of thing is what Eric Lippert would call a vexing exception, and as usual, he knows what he is writing about.Extra love-button clicking for the use of the word "thingy".
Here's the source...
It might be cleaner to return a Maybe, insted of returning null or default(T).
If you are already using this nuget, then yes. Otherwise, assuming you're only working with reference types, It introduces more complexity than a simple
is object
test for reference types.Anyway, this is a good option to keep in mind.