using块有什么用? [英] what is use of using block?

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

问题描述

using块有什么用?当我们为此而去......

谢谢您的进步.

what is use of using block ? when we go for this....

thanks for advance

推荐答案

有时,类包含或使用稀缺资源-货源有限,并且如果您不及时释放它们您的(或其他人)程序可能会失败,因为该应用程序需要资源并且它们都在使用中.示例是Windows中的句柄(其中Windows具有句柄,位图,字体,笔刷等也具有句柄)或SqlConnections,它们受SQL Server安装的限制.

因此,如果您不释放这些资源,则可能会遇到问题.而且很容易忘记:
Sometimes, classes contain or use scarce resources - things which are limited in supply, and that if you don''t release them in a timely manner your (or other peoples) programs can fail because the app needs an resource and they are all in use. Examples are Handles in Windows (where windows have a handle, as do bitmaps, fonts, brushes, and so forth) or SqlConnections which are limited by the SQL server installation.

So if you don''t release these resources, you can get a problem. And it''s very easy to forget:
public void MyMethod()
   {
   MyClassThatUsesAResource mc = new MyClassThatUsesAResource();
   // Do something with the class
   }

在此方法的末尾,mc超出范围,并且不再使用-但您创建的MyClassThatUsesAResource实例仍然存在于堆中,并且直到销毁为止.调用了垃圾收集器来清理内存-可能要 几周 之后!一直以来,类实例正在使用的资源一直在使用中",因为它们尚未被释放.

解决此问题的一种方法是显式发出Dispose:

At the end of this method, mc goes out of scope, and it not used anymore - but the instance of MyClassThatUsesAResource you created is still there on the heap, and it won''t be destroyed until the Garbage Collector is called in to clean up memory - which could be weeks later! All that time, the resources that the class instance is using are "in use" because they haven''t been released.

One way to deal with this is to issue a Dispose explicitly:

public void MyMethod()
   {
   MyClassThatUsesAResource mc = new MyClassThatUsesAResource();
   // Do something with the class
   mc.Dispose();
   }

但是这里有两个可能的问题:
1)很容易忘记,或者通过return退出方法而没有到达Dispose调用.
2)在Dispose调用之后,mc仍然在作用域内,再次使用它不是编译错误-但这会给您一个运行时错误,因为它所引用的对象不再存在! >
为防止这种情况,请使用using块:

But there are two possible problems here:
1) It''s easy to forget, or to exit the method via return and not reach teh Dispose call.
2) After the Dispose call, mc is still in scope, and it is not a compilation error to use it again - but it will give you a run time error, since the object it refers to no longer exists!

To prevent that, use a using block:

public void MyMethod()
   {
   using (MyClassThatUsesAResource mc = new MyClassThatUsesAResource())
      {
      // Do something with the class
      }
   }

现在,无论您如何退出该块,该实例都会自动被Dispose,并且mc同时超出范围,因此在对象被销毁后,您不会意外使用它. br/>
最好对任何实现IDisposable的类始终使用using块!

Now, regardless of how you exit the block, the instance is automatically Disposed, and mc goes out of scope at the same time, so you can''t accidentally use it after the object is destroyed.

It is a very good idea to always use a using block with any class that implements IDisposable!


您有两种用途.

1.使名称空间可用.
You have two uses.

1. To make namespaces available.
using System.Text;



2.自动调用类的析构函数
通常与Stream
一起使用



2. To automatically call the destructor of a class
Usually used with Stream

using (StreamWriter sw = new StreamWriter(@"C:\Temp\Hello.txt"))
{
    sw.WriteLine("Hello World.");
}


无需关闭文件.它是自动完成的.

确保文件已关闭的替代方法是此方法.


No need to close the file. It is done automatically.

The alternative to make sure the file is closed is this.

StreamWriter sw = null;
try
{
  sw = new StreamWriter(@"C:\Temp\Hello.txt")
}
finally
{
  if (sw != null)
    sw.Close();
}


对象实现 IDisposable [^ ]您可以使用using块.
对象通常在需要清理资源(例如,释放文件句柄,关闭数据库连接等)时实现IDisposable.
那时您可以说:
When an object implements IDisposable[^] you may use the using block.
An object usually implements IDisposable when it needs to clean up resources (for example, release a file handle, close a database connection, etc.).
At that point you can say:
using (IDisposable disp = new MyDisposable())
{
   // Some code here...
}

相当于:

IDisposable disp = null;
try
{
   disp = new MyDisposable();
   // Some code here...
finally
{
   if (disp != null)
   {
      disp.Dispose();
   }
}

有关Microsoft文档,请参见使用Statement(C#参考)的MSDN [ ^ ].

For Microsoft documentation see MSDN using Statement (C# Reference)[^].


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

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