Can an object be declared above a using statement instead of in the brackets

后端 未结 5 1563
执念已碎
执念已碎 2021-01-17 08:23

Most of the examples of the using statement in C# declare the object inside the brackets like this:

using (SqlCommand cmd = new SqlCommand(\"SELECT * FROM Cu         


        
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-17 08:51

    It has been answered and the answer is: Yes, it's possible.
    However, from a programmers viewpoint, don't do it! It will confuse any programmer who will be working on this code and who doesn't expect such a construction. Basically, if you give the code to someone else to work on, that other person could end up being very confused if they use the "cmd" variable after the using. This becomes even worse if there's even more lines of code between the creation of the object and the "using" part.

提交回复
热议问题