c#中Using块的目的是什么?它和局部变量有什么不同?
在Using块离开后使用Dispose(),即使代码抛出异常。
因此,您通常将using用于需要在它们之后清理的类,例如IO。
所以,这个使用block:
using (MyClass mine = new MyClass())
{
mine.Action();
}
会做同样的事情:
MyClass mine = new MyClass();
try
{
mine.Action();
}
finally
{
if (mine != null)
mine.Dispose();
}
使用“使用”更简短,更容易阅读。
如果该类型实现了IDisposable,它会自动释放该类型。
考虑到:
public class SomeDisposableType : IDisposable
{
...implmentation details...
}
它们是等价的:
SomeDisposableType t = new SomeDisposableType();
try {
OperateOnType(t);
}
finally {
if (t != null) {
((IDisposable)t).Dispose();
}
}
using (SomeDisposableType u = new SomeDisposableType()) {
OperateOnType(u);
}
第二种更容易阅读和维护。
自从c# 8以来,有了一个新的语法,可以使代码更具可读性:
using var x = new SomeDisposableType();
它没有自己的{}块,使用范围是从声明点到声明它的块的末尾。这意味着你可以避免这样的事情:
string x = null;
using(var someReader = ...)
{
x = someReader.Read();
}
还有这个:
using var someReader = ...;
string x = someReader.Read();
从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.
换句话说,using语句告诉. net一旦不再需要using块中指定的对象,就释放它。
using (B a = new B())
{
DoSomethingWith(a);
}
等于
B a = new B();
try
{
DoSomethingWith(a);
}
finally
{
((IDisposable)a).Dispose();
}
using语句用于处理c#中实现IDisposable接口的对象。
IDisposable接口有一个名为Dispose的公共方法,用于释放对象。当我们使用using语句时,我们不需要在代码中显式地处理对象,using语句会处理它。
using (SqlConnection conn = new SqlConnection())
{
}
当我们使用上面的代码块时,内部生成的代码是这样的:
SqlConnection conn = new SqlConnection()
try
{
}
finally
{
// calls the dispose method of the conn object
}
更多细节请阅读:理解c#中的“using”语句。
推荐文章
- 返回文件在ASP。Net Core Web API
- 自定义HttpClient请求头
- 如果我使用OWIN Startup.cs类并将所有配置移动到那里,我是否需要一个Global.asax.cs文件?
- VS2013外部构建错误"error MSB4019: The imported project <path> was not found"
- 从另一个列表id中排序一个列表
- 等待一个无效的异步方法
- 无法加载文件或程序集…参数不正确
- c#中枚举中的方法
- Javascript函数前导bang !语法
- 如何从字符串中删除新的行字符?
- 如何设置一个默认值与Html.TextBoxFor?
- 检查属性是否有属性
- 格式化XML字符串以打印友好的XML字符串
- 返回内容与IHttpActionResult非ok响应
- 从IEnumerable<KeyValuePair<>>重新创建字典