Does ADO.NET go overboard with IDisposable?

后端 未结 3 1070
孤独总比滥情好
孤独总比滥情好 2021-01-24 05:24

When .NET first came out, I was one of many who complained about .NET\'s lack of deterministic finalization (class destructors being called on an unpredictable schedule). The co

3条回答
  •  渐次进展
    2021-01-24 06:18

    If a class implements IDisposable, then you should always make sure that it gets disposed, without making any assumptions about its implementation.

    I think that's the minimal amount of code that you could possibly write that ensures that your objects will be disposed. I see absolutely no problem with it, and it does not distract me at all from the main logic of my code.

    If reducing this code is of paramount importance to you, then you can come up with your own replacement (wrapper) of SqlConnection whose child classes do not implement IDisposable and instead get automatically destroyed by the connection once the connection is closed. However, that would be a huge amount of work, and you would lose some precision with regards to when something gets disposed.

提交回复
热议问题