I have seen so many times developers using a disposable object inline, here for instance. By inline I mean:
var result = new DataTable().Compute("1 + 4 * 7", null);
I know that the Dispose
method won't be called, but since no reference is held to the object, how will the garbage collector handle it? Is it safe to use a disposable object like that?
I used a DataTable
in my example because it is the only concrete example I found, but my question applies to disposable objects in general. I do not personally use them like that, I just wanted to know if they are handled diffently by the GC if they are used that way.