使用的目的是什么? [英] What is the purpose of Using?

查看:214
本文介绍了使用的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DUPE:<一href="http://stackoverflow.com/questions/75401/uses-of-using-in-c">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.

这篇关于使用的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆