Bad practice? Non-canon usage of c#'s using statement

后端 未结 6 1802
广开言路
广开言路 2021-01-01 16:36

C# has the using statement, specifically for IDisposable objects. Presumably, any object specified in the using statement will hold some sort of re

6条回答
  •  失恋的感觉
    2021-01-01 16:56

    I recommend against it; my belief is that code is to effectively communicate with the maintainer of the code, not the compiler, and should be written with the maintainer's comprehension in mind. I try to use "using" only to dispose of a resource, typically an unmanaged resource.

    I am in a minority. Most people it seems use "using" as a general purpose "I want some cleanup code to run even if an exception is thrown" mechanism.

    I dislike this because (1) we already have a mechanism for that, called "try-finally", (2) it uses a feature for a purpose it was not intended for, and (3) if the call to the cleanup code is important, then why isn't it visible at the point where it is called? If it is important then I want to be able to see it.

提交回复
热议问题