什么是C#中使用块我为什么要使用它呢? [英] What is the C# Using block and why should I use it?
本文介绍了什么是C#中使用块我为什么要使用它呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是在C#中使用块的目的是什么?它是如何从一个局部变量有什么不同?
What is the purpose of the Using block in C#? How is it different from a local variable?
推荐答案
如果该类型实现IDisposable,它会自动部署它。
If the type implements IDisposable, it automatically disposes it.
假设:
public class SomeDisposableType : IDisposable
{
...implmentation details...
}
这是等同的:
SomeDisposableType t = new SomeDisposableType();
try {
OperateOnType(t);
}
finally {
t.Dispose();
}
using (SomeDisposableType u = new SomeDisposableType()) {
OperateOnType(u);
}
二是更容易阅读和维护。
The second is easier to read and maintain.
这篇关于什么是C#中使用块我为什么要使用它呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文