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

后端 未结 6 1846
广开言路
广开言路 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:51

    Just because you can (or because Phil Haack says it's okay), doesn't mean you should.

    The basic rule of thumb: if I can read your code and understand what it's doing and what your intent was, then it's acceptable. If, on the other hand, you need to explain what you did, or why you did it, it's probably going to trip up junior developers maintaining the code.

    There are many other patterns that can accomplish this with better encapsulation.

    The bottom line: this "technique" buys you nothing and only acts to confuse other developers.

提交回复
热议问题