Is it ok not to dispose a MemoryStream / StringReader?

前端 未结 3 1521
轮回少年
轮回少年 2021-01-13 15:42

I would like to create a method that returns an XmlReader. Depending on the circumstances the XmlReader may be fed different types of streams, either a StringReader or a Mem

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-13 16:16

    Nothing is going to suffer in this case, but IMO it is still very bad practice. You own them - why not do it properly? Actually, disposing a MemoryStream still can't deallocate etc - it is still bound by GC. But there is a distinct code smell in here somewhere. This can become real problems if anything ever changes, and suddenly it isn't a MemoryStream but something else, etc.

    We can't make you dispose it, but personally: I am fastidious about my usings

提交回复
热议问题