using(IDisposable obj = new …) in C# to write code blocks in stream (e.g. XML)

前提是你 提交于 2019-12-05 05:50:50

The main downside I see (apart from the non-standard use of the using statement arguably violating the "principle of least surprise") is that it will attempt to repeatedly write all the nested end tags in the event of an exception thrown by your XmlWriter.

In theory at least, you could have an exception thrown while writing an inner end tag, followed by successful writes of outer end tags in the "finally" blocks generated by the using statements. This would lead to invalid output.

Is there any down side to this,

No. Destructors (finalizers) should be avoided anyway, even a class with resources can usually do (better) without.

or is this a good way to maintain a correct nesting of elements?

Yes. You can use System.Web.Mvc.Html.MvcForm as a reference.

these IDisposable do not implement the sophisticated pattern recommended by Microsoft

That 'full' pattern is correct but dated. It only describes the situation for a 'bare' unmanaged resource. An official references for dealing with managed resources only is not provided, unforunately.

The complex pattern used by microsoft is created to ensure that unmanaged resources are freed, even if you don't call Dispose().

You don't use any unmanaged resources in your classes. You simply take advantage of C#'s using keyword to make your code more readable and maintainable. I think that's a nice approach and i have also used it in the past.

I think it would not even make sense to use the finalizer construct, since you need to be sure that the end tag gets written at the correct position. You never know when the finalizer is called, so you could not be sure when the end tag would be written if you forget to Dispose your element. Your Xml document would still be messed up, whether the end tag is not written at all or at the wrong position.

In the worst case, when the finalizer calls Dispose(), the XmlWriter could already be disposed and you get an Exception. So better no finalizer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!