简体   繁体   English

C#System.Net.NetworkCredential

[英]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. 一切将由垃圾收集器清理。

From MSDN : MSDN

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.

相关问题 在c#的“ System.Net.NetworkCredential”中使用未过载的webclient输入网页身份验证 - enter webpage authentication with webclient that's not overloaded in “System.Net.NetworkCredential” in c# 如何正确地将 System.Net.NetworkCredential 传递给 WCF 客户端? - How do I properly pass System.Net.NetworkCredential to WCF Client? c# NetworkCredential 和 System.Net.WebRequest 存储在代码中加密 - c# NetworkCredential and System.Net.WebRequest store crypted in code PHP中的C#NetworkCredential - C# NetworkCredential in PHP C#ASP .NET; 获取登录用户的NetworkCredential对象? - C# ASP .NET; Get the NetworkCredential Object for the logged in user? C#替换来自NetworkCredential的邮件 - C# replace the mail from NetworkCredential 获取当前用户的 NetworkCredential (C#) - Getting NetworkCredential for current user (C#) C#中的NetworkCredential在分配给HttpRequest对象时如何工作? - How does NetworkCredential in C# works when assigned to a HttpRequest object? ASP.NET中的NetworkCredential错误 - NetworkCredential error in ASP.NET 为什么使用“新的NetworkCredential(用户名,密码)”不适用于我的网站的基本身份验证(来自WinForms C#应用程序)? - why is use of “new NetworkCredential(username, password)” not working for Basic Authentication to my website (from a WinForms C# app)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM