Determine managed vs unmanaged resources

前端 未结 3 543
别那么骄傲
别那么骄傲 2020-12-28 17:57

There are lots of questions about managed vs unmanaged resources. I understand the basic definition of the two. However, I have a hard time knowing when a resource or obje

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-28 18:46

    A FileStream must use unmanaged resources, but when I implement the IDisposable pattern, should I consider this a managed or unmanaged resources?

    A FileStream is a managed resource.

    Managed resources are classes that contain (and must manage) unmanaged resources. Usually the actual resource is several layers down.

    I've been assuming thus far that if the object implements IDisposable, then it is managed.

    Correct.

    How would I know that IntPtr should be handled as an unmanaged resoruce?

    From the documentation of the API that you got its value from. But do note that in practice, most programmers never deal with unmanaged resources directly. And when you do have to, use the SafeHandle class to turn an unmanaged resource into a managed resource.

提交回复
热议问题