c#中Using块的目的是什么?它和局部变量有什么不同?
当前回答
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”语句。
其他回答
它实际上只是一些语法糖,不需要对实现IDisposable的成员显式调用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块中放置代码可以确保一旦控件离开块,对象就会被处理(尽管不一定会被收集)。
还要注意,通过using实例化的对象在using块中是只读的。请参考这里的官方c#参考。
推荐文章
- 防止在ASP中缓存。NET MVC中使用属性的特定操作
- 转换为值类型'Int32'失败,因为物化值为空
- c#中有任何连接字符串解析器吗?
- 在Linq中转换int到字符串到实体的问题
- 是否可以动态编译和执行c#代码片段?
- 创建自定义MSBuild任务时,如何从c#代码获取当前项目目录?
- MSBuild路径
- c#和Java的主要区别是什么?
- 在c#中创建一个特定时区的DateTime
- .NET中的属性是什么?
- csproj文件中的“Service Include”是干什么用的?
- 如何使用try catch进行异常处理是最佳实践
- 替换字符串中第一次出现的模式
- .NET中字节的字面后缀?
- 如何处理AccessViolationException