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();
(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:
public static void NoOp(this T value)
{
// Do nothing.
}
Then call:
instance.SomeProperty.NoOp();
That won't box the value or touch it at all - just call the getter. Another advantage of this over ToString
is 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...