In the general case, no. Because: side-effects. In general, any.Method(args)
can do something different every time (logging, randomness, incrementing counters, connecting to an external unmanaged resource, etc), and must be called each time to retain the original semantic. It could be that a white-list of "pure" functions is maintained, but I can think of no special reason to include int.Parse
in that.
Basically, if you want to hoist it: hoist it yourself. The code in your second example is reasonable (personally I'd add an else
, i.e else if
).
Additionally: your example calls SomeStringProperty
multiple times : that too must be called multiple times to retain the semantic, and could behave differently.
Finally, in a multi-threaded world, the field itself could have changed.