术语"非托管资源"通常用于描述not directly under the control of the garbage collector种情况.例如,如果打开与数据库服务器的连接,这将使用服务器上的资源(用于维护连接)以及其他可能的非连接.net资源,如果提供程序不是完全用托管代码编写的.
这就是为什么对于数据库连接之类的东西,建议您这样编写代码:
using (var connection = new SqlConnection("connection_string_here"))
{
// Code to use connection here
}
因为这样可以确保对Connection对象调用.Dispose()
,从而确保清理所有非托管资源.