9

I didn't understand what is IntPtr, could someone explain this? thanks

lital maatuk
  • 5,921
  • 20
  • 57
  • 79

6 Answers6

10

It is an integer that is the same size as a pointer. 32 bits wide in 32 bit images, 64 wide in 64 bit images.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
9

It is the managed counterpart of void*.

You can cast to and from void* for usage in managed code without having to resort to unsafe code in managed layers, eg C#.

leppie
  • 115,091
  • 17
  • 196
  • 297
2

It's a .NET platform-specific type that is used to represent a pointer or a handle.

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.

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.FileStream class to hold file handles.

(from MSDSN)

DVK
  • 126,886
  • 32
  • 213
  • 327
1

This is about the c and c++ type intptr_t but the principle is the same. What is uintptr_t data type

Community
  • 1
  • 1
jcoder
  • 29,554
  • 19
  • 87
  • 130
0

http://msdn.microsoft.com/en-us/library/system.intptr(v=VS.100).aspx#Y69

Xorty
  • 18,367
  • 27
  • 104
  • 155
  • At least pretend to answer the question by copying and pasting the relevant information from the docs. A link is not sufficient to qualify as an answer. – Cody Gray - on strike Feb 05 '11 at 09:52
  • I wanted to show author how trivial is finding answer to such a question. – Xorty Feb 05 '11 at 17:18
  • Well, the answers from people here are a lot more clear then the msdn which i have tried to read before i put the question. Thanks for the effort and good will anyway. – lital maatuk Feb 05 '11 at 18:01
0

A pointer sized blackbox. Sometimes you have languages that don't support unsafe code/pointers, and thus need to use IntPtr in the API.

I think its use has been reduced since .net 2 since many of its use-cases are better fit for safe-handles.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262