using语句可以用大括号代替吗?
我对 SqlConnection
使用 using 语句。这对性能有好处,因为强制调用 Dispose() 会更快地释放到池的连接。
但是,我意识到使用中创建的对象无法重新定义。我不能这样做:
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
//...
connection = new SqlConnection(connectionString2);
//...
connection = new SqlConnection(connectionString3);
}
我想知道是否可以替换使用,并执行如下操作:
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
//...
connection = new SqlConnection(connectionString2);
//...
connection = new SqlConnection(connectionString3);
}
在最后一个大括号之后,SqlConnection
将无法访问。当对象超出范围时会立即调用 Dispose() 吗?
I use the using statement for SqlConnection
. It's is good for performance because forces calling Dispose() that simply releases the connection to the pool sooner.
However, I realized that object created in using cannot be redefined. I cannot do like this:
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
//...
connection = new SqlConnection(connectionString2);
//...
connection = new SqlConnection(connectionString3);
}
I was wondering if I can replace using, and do something like this:
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
//...
connection = new SqlConnection(connectionString2);
//...
connection = new SqlConnection(connectionString3);
}
The SqlConnection
will not be accesible after last }
brace. Will be the Dispose() called immediatly when object goes out of scope?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不,在您的第二个示例中,事情不会自动清理(事实上,使用您拥有的代码,您将保留多个连接保持打开状态)。
不仅如此,如果在
using
块内抛出异常,您还会失去自动清理功能。请记住,using
块分解为:如果您确实使用不同的连接,并且每个连接在块的末尾被 Dispose of,我将使用多个 using 块:
No, things won't get automatically cleaned up in your second example (in fact, with the code you have, you'll leave several connections hanging open).
Not only that, but you lose the automatic cleanup in case of Exceptions being thrown inside the
using
block. Remember that ausing
block decomposes into:If you're really using different connections and each connection is Disposed of at the end of the block, I would use several using blocks:
using
语句是一种语法糖,它对()
中初始化的对象调用Dispose
,因此您不能简单地将其替换为您所拥有的在你的例子中。您将注意到,您可以在
using
语句中使用的唯一对象是那些实现了IDisposable
的对象,这确保了可以调用Dispose
。正如 这篇 文章所述,编译器会将其转换为:
至此:
所以,除非您复制此结构,否则您将不会获得相同的行为。
另外 - 按照示例中的方式重用变量是不好的做法。阅读代码的人可能会认为他们正在查看连接 1,而实际上他们正在查看连接 2 或 3。最终可能会非常混乱并导致各种问题。
The
using
statement is syntactic sugar that callsDispose
on the objects initialized within the()
, so you can't simply replace it as you have in your example.You will note that the only objects that you can use within a
using
statement are those that implementIDisposable
, which ensures thatDispose
can be called.As this article explains, the compiler will transform this:
To this:
So, unless you duplicate this structure, you won't get the same behavior.
In addition - reusing a variable the way you are in your example is bad practice. Someone who reads the code may think they are looking at connection 1 when they are in fact looking at 2 or 3. Could end up very confusing and cause all kinds of problems down the road.
不,在右大括号之后不会调用它。您需要手动调用它或使用
using
语句。如果不执行dispose,那么它将在调用finalize时执行。这里有两个问题:
No, it won't be called after the closing curly brace. You need to call it manually or use a
using
statement.If you don't execute the dispose then It will be executed when the finalize is called. And here you have 2 problems
不,使用创建了一些特定的清理结构,而仅使用大括号是无法获得这些结构的。
如果您使用 Reflector 并查看 IL,则会在其目标的 using 块末尾添加对 Dispose 的调用:
这解释了为什么您无法在 using 块内创建新连接并将其分配给变量 - 这样做因此会使原始参考挂起且未处理。
No, using creates some specific clean-up constructs that you won't get with just the braces.
If you use Reflector and look at the IL there's a call to Dispose added at the end of the using block for its target:
This explains why you can't create a new connection inside the using block and assign it to the variable -- doing so would leave the original reference hanging and undisposed.
...是一点糖,翻译成:
然而:
...不翻译成任何东西。只是::
D
编辑:编辑时请不要破坏格式:(
...is a bit of sugar which translates into:
Whereas:
...doesn't translate into anything. It's just:
:D
Edit: Please don't destroy formatting when you edit :(
不,你不能这样做。至少在 C# 中是这样。
但是您可以在一个 using 语句中创建不同的一次性对象:
C++/CLI 您可以以类似堆栈的方式使用您的一次性类:
No, you can't do this. At least in C#.
But you can create different disposable objects inside one using statement:
C++/CLI you may use your disposable classes in stack-like faision:
我一开始也这么认为......但显然当 using 块结束时,会在您的对象上调用
.Dispose()
,这与让对象超出范围不同。由于 C# 是一种垃圾收集语言,因此实际清理对象可能需要一些时间。using
块确保它被立即处理,无论是否出现任何异常。I thought this too at first... but apparently when the using block ends,
.Dispose()
is called on your object, which is different than letting the object go out of scope. Because C# is a garbage collected language, it could take time before your object is actually cleaned up. Theusing
block ensures that it is disposed of immediately, and regardless of any exceptions.