I would like to know the simplest code for polling a property’s value, to execute the code in its getter.
Currently I’m using: instance.property.ToString();, but I’d rather have something without possible side-effects or unnecessary overhead.
I would like to know the simplest code for polling a property’s value, to
Share
(I’m assuming you’re trying to avoid the warning you get from simply assigning the value to an unused variable.)
You could write a no-op extension method:
Then call:
That won’t box the value or touch it at all – just call the getter. Another advantage of this over
ToStringis that this won’t go bang if the value is a null reference.It will require JIT compilation of the method once per value type, but that’s a pretty small cost…