Just what is an IntPtr exactly?
It's a value type large enough to store a memory address as used in native or unsafe code, but not directly usable as a memory address in safe managed code.
You can use IntPtr.Size
to find out whether you're running in a 32-bit or 64-bit process, as it will be 4 or 8 bytes respectively.
Here's an example:
I'm writing a C# program that interfaces with a high-speed camera. The camera has its own driver that acquires images and loads them into the computer's memory for me automatically.
So when I'm ready to bring the latest image into my program to work with, the camera driver provides me with an IntPtr to where the image is ALREADY stored in physical memory, so I don't have to waste time/resources creating another block of memory to store an image that's in memory already. The IntPtr just shows me where the image already is.
It's a "native (platform-specific) size integer." It's internally represented as void*
but exposed as an integer. You can use it whenever you need to store an unmanaged pointer and don't want to use unsafe
code. IntPtr.Zero
is effectively NULL
(a null pointer).