ScanIAm wrote: I've done this myself, but how 'safe' is it:
The part that concerns me is the accessing of the function 'Length' when you don't really know that the new String has actually been created.
You absolutely know that the new String has been created before Length is called. An exception will be thrown if the String could not be created.
[Edge case ahead.]
Now, in the case of
the instance of MyObject
may be garbage collected before LongRunningMethod returns. This usually doesn't cause a problem, but it can; to cause a problem usually involves a questionable design decision (e.g., a disposed
MyObject cleans up a field, and this
happens to occur after LongRunningMethod's last reference to your
MyObject instance but before using a locally stored reference to that field). I can make it happen but I don't expect to see it in the wild.
The MyObject instance won't be collected by the GC if you are accessing any fields that held by that instance... otherwise - as said - it would be a heavy CLR (GC) bug!