问题描述
DUPE:http://stackoverflow.com/questions/75401/uses-of-using-in-c
我看到人们使用下面的和我想知道什么是它的目的是什么?就那么垃圾回收使用后的对象被销毁?
例如:
使用(有什么mySomething =新的东西()){
mySomething.someProp =嗨;
}
使用进行翻译,大致为:
东西mySomething =新的东西();
尝试
{
something.someProp =嗨;
}
最后
{
如果(mySomething!= NULL)
{
mySomething.Dispose();
}
}
这就是pretty的是它。其目的是支持确定性处理,一些C#不有,因为它是一个垃圾收集的语言。在使用/处置模式给程序员一种指定什么时候一个类型清理其资源。
DUPE: http://stackoverflow.com/questions/75401/uses-of-using-in-c
I have seen people use the following and I am wondering what is its purpose?Is it so the object is destroyed after its use by garbage collection?
Example:
using (Something mySomething = new Something()) {
mySomething.someProp = "Hey";
}
Using translates, roughly, to:
Something mySomething = new Something();
try
{
something.someProp = "Hey";
}
finally
{
if(mySomething != null)
{
mySomething.Dispose();
}
}
And that's pretty much it. The purpose is to support deterministic disposal, something that C# does not have because it's a garbage collected language. The using / Disposal patterns give programmers a way to specify exactly when a type cleans up its resources.
这篇关于使用的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!