M
Michael C#
David said:OK, a simple answer. Disposing a SqlCommand does absolutely nothing
except take up time. It's a no-op.
Thank you! Finally a straight answer! My next question is this: is the
optimizer smart enough to figure this out and discard the no-op?
I see your general point here, but in practice it's not that tough. In
day-to-day programming, I pretty much deal with only four types of
objects:
Web controls, which never need to be disposed.
Forms controls, which should always be disposed.
Data library objects, and since there's only a few of them I tend to
know the ones I'm working with pretty damn well since I'm hitting the db
all day long, and so I have a pretty good sense as to whether I want to
dispose them and when.
Things that derive from Component, which generally don't need to be
disposed, but it only takes a moment to figure out if the class actually
implements a Dispose function, so in practice that's not a problem.
Now see, this is where the confusion comes in. You say Forms controls
should always be disposed - yet others say Dispose is not necessary on a
Label... Is the Label an "exception"? Or should Forms controls, including
the Label, be disposed?
Also, at the end you mention "it only takes a moment to figure out if the
class actually implements a Dispose function..." which takes us full circle
to the crux of the matter - some say that not everything that implements a
Dispose needs to be Disposed. The question is which Components that expose
a Dispose method need to have Dispose called on them?
I actually think avoiding the Dispose calls makes code a lot cleaner.
That may be true, but I think it may be short-term thinking. Per another
message in here, if the SqlCommand, for instance, were to be implemented
differently in the future - and the Developer suddenly recommends the use of
Dispose - you will have to re-tool your design patterns and re-work your
code.