[英]c# System.Net.NetworkCredential
System.Net.NetworkCredential
does not have a Dispose
method. System.Net.NetworkCredential
没有Dispose
方法。 What is the best way to handle properly disposing an object created using this class? 正确处理使用此类创建的对象的最佳方法是什么?
JamesNT 詹姆斯·NT
You could just set the object reference to null in the finally section of a try. 您可以在尝试的最后一部分中将对象引用设置为null。
var myCredential = new NetworkCredential();
try
{
//Do stuff here
}
finally
{
myCredential = null;
}
However, the .NET garbage collection should mark your object reference for collection as soon as it becomes out of scope. 但是,.NET垃圾回收一旦超出范围,应将您的对象引用标记为要收集。
If an object does not implement IDisposable
then the proper way to "dispose" of it when you are done using it, is to let it go out of scope. 如果对象没有实现
IDisposable
那么在使用完该对象后“处置”该对象的正确方法是使其超出范围。
IDisposable
is designed to allow the object to clean up any unmanaged resources it might be holding. IDisposable
旨在允许对象清除它可能持有的任何非托管资源。 If the object only contains managed resources, then there is no reason to dispose of it; 如果对象仅包含托管资源,则没有理由处置它; everything will be cleaned up by the garbage collector.
一切将由垃圾收集器清理。
The primary use of this interface is to release unmanaged resources.
该接口的主要用途是释放非托管资源。 The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used.
当不再使用托管对象时,垃圾收集器会自动释放分配给该对象的内存。 However, it is not possible to predict when garbage collection will occur.
但是,无法预测何时会发生垃圾回收。 Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.
此外,垃圾收集器不了解诸如窗口句柄或打开的文件和流之类的非托管资源。
Use the Dispose method of this interface to explicitly release unmanaged resources in conjunction with the garbage collector.
使用此接口的Dispose方法与垃圾回收器一起显式释放非托管资源。 The consumer of an object can call this method when the object is no longer needed.
当不再需要该对象时,该对象的使用者可以调用此方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.