处置时,托管和非托管资源之间的区别是什么? (。净) [英] What is the difference between managed and native resources when disposing? (.NET)

查看:174
本文介绍了处置时,托管和非托管资源之间的区别是什么? (。净)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读关于如何实现IDisposable 的 MSDN文章,我不确定之间的差异管理和引用的文章本地资源。

I was reading the MSDN article about how to implement IDisposable and I am uncertain about the difference between managed and native resources cited in the article.

我有处置时必须配置2的字段的类。我应该把他们当作管理或本地资源(配置=真时只处理)?

I have a class that must dispose 2 of its fields when it is disposed. Should I treat them as Managed (dispose only when disposing = true) or Native resources?

推荐答案

一个管理资源是另一种托管类型,它实现的IDisposable 。你需要调用的Dispose()上的任何其他的IDisposable 键入您使用。原生资源的管理世界以外的任何东西,例如原生Windows处理等。

A managed resource is another managed type, which implements IDisposable. You need to call Dispose() on any other IDisposable type you use. Native resources are anything outside the managed world such as native Windows handles etc.


修改:答案在评论质疑(太长的评论)

EDIT: Answer to question in comment (too long for comment)

没有,这只是一个托管类型。一个正确的构造类型,它没有实现的IDisposable 将被垃圾收集处理,你不必做任何事情。如果你的类型直接使用本地资源(例如,通过调用Win32的库),则必须实现的IDisposable 在你的类型,并在<$ C $处置资源(县) C>处置方法。如果你的类型使用由另一种类型,它实现的IDisposable 封装本地资源,必须调用的Dispose()上的实例这种类型在你的类型的处置方法。

No that is just a managed type. A correctly constructed type, which doesn’t implement IDisposable will be handled by the garbage collector and you don’t have to do anything else. If your type uses a native resource directly (e.g. by calling Win32 libraries), you must implement IDisposable on your type and dispose of the resource(s) in the Dispose method. If your type uses a native resource encapsulated by another type which implements IDisposable, you must call Dispose() on instances of this type in the Dispose method of your type.

这篇关于处置时,托管和非托管资源之间的区别是什么? (。净)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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