Proper IntPtr use in C#

后端 未结 4 1359
生来不讨喜
生来不讨喜 2020-12-31 08:36

I think I understand the use of IntPtr, though I\'m really not sure.

I copied the IDisposable pattern from MSDN just to see what I could get from it, and w

4条回答
  •  醉酒成梦
    2020-12-31 09:02

    IntPtr is an integer the size of a pointer, 32 bits on 32 bit systems and 64 bits on 64 bit systems. It is typically used to wrap a pointer or handle to be handed off to an unmanaged function as you have done. "unsafe" means you are using pointers in your C# code, so IntPtrs outside of unsafe blocks or with out allowing unsafe code to be compiled.

    I also can't tell you what the point of that component but it really, really shouldn't exist. Handles should be owned by an object the exposes the functionality that the handle represents and be responsible for managing that handle's life cycle. A class that just arbitrarily closes handles that it didn't allocate is frighteningly bad design.

提交回复
热议问题