Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Just what is an IntPtr exactly?

Tags:

c#

intptr

Through using IntelliSense and looking at other people's code, I have come across this IntPtr type; every time it has needed to be used I have simply put null or IntPtr.Zero and found most functions to work. What exactly is it and when/why is it used?

like image 386
Callum Rogers Avatar asked Jul 18 '09 18:07

Callum Rogers


People also ask

When to use IntPtr?

uintptr is used when you're dealing with pointers, it is a datatype that is large enough to hold a pointer. It is mainly used for unsafe memory access, look at the unsafe package. *uint is a pointer to an unsigned integer.

What does IntPtr do?

The IntPtr type can be used by languages that support pointers and as a common means of referring to data between languages that do and do not support pointers. IntPtr objects can also be used to hold handles. For example, instances of IntPtr are used extensively in the System. IO.

What is IntPtr in C?

The IntPtr type is designed to be an integer whose size is platform-specific. That is, an instance of this type is expected to be 32-bits on 32-bit hardware and operating systems, and 64-bits on 64-bit hardware and operating systems.

What IntPtr zero?

You cannot assign null to a value-type. A reference-type can be null, as in, not referring to an object instance, but a value-type always has a value. IntPtr. Zero is just a constant value that represents a null pointer.


Video Answer


1 Answers

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).

like image 138
Sam Harwell Avatar answered Sep 23 '22 21:09

Sam Harwell