I have a windows form that doesn’t have any events or properties I wish to access from the owner. There are two ways I can open the form:
frmExample ex = new frmExample();
ex.ShowDialog(this);
and
(new frmExample()).ShowDialog(this);
Will there be differences in terms of memory allocation and such? Are there any implications, pros and cons? Personally, possibly naively, I prefer the second approach.
Thanks
One big difference is that you won’t be able to Dispose() the form instance. You should, disposal is not automatic when you call ShowDialog(), only when you call Show(). Boilerplate code is:
You can perhaps see from this snippet why the form doesn’t get disposed automatically. It would risk generating ObjectDisposedException when you access the properties. You have to dispose it yourself after you’re done accessing the properties. The using statement makes it automatic and exception-safe.