Using various types in a 'using' statement (C#)

前端 未结 4 2141
陌清茗
陌清茗 2020-12-03 06:48

Since the C# using statement is just a syntactic sugar for try/finally{dispose}, why does it accept multiple objects only if they are of the same type?

4条回答
  •  旧时难觅i
    2020-12-03 07:15

    There's no particularly good technical reason; we could have come up with a syntax that allowed multiple declarations of nonhomogeneous types. Given that we did not, and there already is a perfectly good, clear, understandable and fairly concise mechanism for declaring nested using blocks of different types, we're unlikely to add a new syntactic sugar just to save a few keystrokes.

提交回复
热议问题