什么是 C# Using 块,为什么要使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/212198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the C# Using block and why should I use it?
提问by Ryan Michela
What is the purpose of the Using
block in C#? How is it different from a local variable?
Using
C#中块的目的是什么?它与局部变量有何不同?
采纳答案by plinth
If the type implements IDisposable, it automatically disposes it.
如果类型实现 IDisposable,它会自动处理它。
Given:
鉴于:
public class SomeDisposableType : IDisposable
{
...implmentation details...
}
These are equivalent:
这些是等效的:
SomeDisposableType t = new SomeDisposableType();
try {
OperateOnType(t);
}
finally {
if (t != null) {
((IDisposable)t).Dispose();
}
}
using (SomeDisposableType u = new SomeDisposableType()) {
OperateOnType(u);
}
The second is easier to read and maintain.
第二个更容易阅读和维护。
回答by Sam
Using
calls Dispose()
after the using
-block is left, even if the code throws an exception.
Using
Dispose()
在using
离开 -block之后调用,即使代码抛出异常。
So you usually use using
for classes that require cleaning up after them, like IO.
所以你通常using
用于需要在它们之后清理的类,比如 IO。
So, this using block:
所以,这使用块:
using (MyClass mine = new MyClass())
{
mine.Action();
}
would do the same as:
会做同样的事情:
MyClass mine = new MyClass();
try
{
mine.Action();
}
finally
{
if (mine != null)
mine.Dispose();
}
Using using
is way shorter and easier to read.
使用using
更短,更容易阅读。
回答by TheSmurf
Placing code in a using block ensures that the objects are disposed (though not necessarily collected) as soon as control leaves the block.
将代码放在 using 块中可确保一旦控制离开块,对象就被释放(尽管不一定收集)。
回答by Robert S.
From MSDN:
来自 MSDN:
C#, through the .NET Framework common language runtime (CLR), automatically releases the memory used to store objects that are no longer required. The release of memory is non-deterministic; memory is released whenever the CLR decides to perform garbage collection. However, it is usually best to release limited resources such as file handles and network connections as quickly as possible.
The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.
C# 通过 .NET Framework 公共语言运行时 (CLR) 自动释放用于存储不再需要的对象的内存。内存的释放是不确定的;每当 CLR 决定执行垃圾收集时,就会释放内存。但是,通常最好尽快释放有限的资源,例如文件句柄和网络连接。
using 语句允许程序员指定使用资源的对象何时应该释放它们。提供给 using 语句的对象必须实现 IDisposable 接口。该接口提供了 Dispose 方法,该方法应该释放对象的资源。
In other words, the using
statement tells .NET to release the object specified in the using
block once it is no longer needed.
换句话说,该using
语句告诉 .NET 在using
不再需要块中指定的对象时将其释放。
回答by SaaS Developer
It is really just some syntatic sugar that does not require you to explicity call Dispose on members that implement IDisposable.
它实际上只是一些语法糖,不需要您对实现 IDisposable 的成员显式调用 Dispose。
回答by Bert Huijben
using (B a = new B())
{
DoSomethingWith(a);
}
is equivalent to
相当于
B a = new B();
try
{
DoSomethingWith(a);
}
finally
{
((IDisposable)a).Dispose();
}
回答by Meera Tolia
The using statement obtains one or more resources, executes a statement, and then disposes of the resource.
using 语句获取一个或多个资源,执行一条语句,然后处置该资源。
回答by Sunquick
The using statement is used to work with an object in C# that implements the IDisposable
interface.
using 语句用于处理 C# 中实现IDisposable
接口的对象。
The IDisposable
interface has one public method called Dispose
that is used to dispose of the object. When we use the using statement, we don't need to explicitly dispose of the object in the code, the using statement takes care of it.
该IDisposable
接口调用Dispose
了一个公共方法,用于处理对象。当我们使用 using 语句时,我们不需要在代码中显式地处理对象, using 语句会处理它。
using (SqlConnection conn = new SqlConnection())
{
}
When we use the above block, internally the code is generated like this:
当我们使用上面的块时,内部生成的代码是这样的:
SqlConnection conn = new SqlConnection()
try
{
}
finally
{
// calls the dispose method of the conn object
}
For more details read: Understanding the 'using' statement in C#.
有关更多详细信息,请阅读:了解 C# 中的“使用”语句。