IntPtr Struct

Represents a platform-specific handle or pointer.

The IntPtr structure is used to represent a pointer or a handle. A pointer is a variable that stores the memory address of another variable. A handle is an identifier that Windows uses to identify an object such as a file, a font, or a bitmap.

The size of the IntPtr structure depends on the platform. On a 32-bit platform, a IntPtr is 32 bits in size, and on a 64-bit platform, it is 64 bits in size.

Namespace

System

Assembly

System.Runtime.dll

Syntax

public struct IntPtr

Remarks

Use the IntPtr structure when you need to pass handles or pointers between managed code and native code.

The IntPtr.Zero field represents a null pointer.

Members

Fields

Name Description
Zero A new instance of the IntPtr structure whose value is 0.

Constructors

Name Description
IntPtr(int value) Initializes a new instance of the IntPtr structure, using the specified 32-bit integer. This constructor is only supported on 32-bit platforms.
IntPtr(long value) Initializes a new instance of the IntPtr structure, using the specified 64-bit integer. This constructor is only supported on 64-bit platforms.

Methods

Name Description
Equals(object obj) Determines whether the specified object is a IntPtr structure that has the same value as this instance.
GetHashCode() Returns the hash code for the current instance.
Size Gets the size of an IntPtr on the current platform.
ToString() Converts the value of this instance to its equivalent string representation.
ToInt32() Converts the value of this instance to a 32-bit signed integer. This method is only supported on 32-bit platforms.
ToInt64() Converts the value of this instance to a 64-bit signed integer. This method is only supported on 64-bit platforms.

Operators

Name Description
== Compares two IntPtr structures for equality.
!= Compares two IntPtr structures for inequality.
++ Increments the value of an IntPtr structure.
-- Decrements the value of an IntPtr structure.
op_Explicit(long value) Converts a 64-bit signed integer to an IntPtr.
op_Explicit(int value) Converts a 32-bit signed integer to an IntPtr.
op_Explicit(IntPtr value) Converts an IntPtr to a Pointer.

Example

The following example demonstrates how to create an IntPtr, check its size, and convert it to a string.

C#:


using System;

public class Example
{
    public static void Main(string[] args)
    {
        // Create an IntPtr on the current platform
        IntPtr ptr = new IntPtr(12345);
        Console.WriteLine($"IntPtr value: {ptr}");

        // Get the size of IntPtr
        Console.WriteLine($"Size of IntPtr: {IntPtr.Size} bytes");

        // Check if it's the zero IntPtr
        if (ptr == IntPtr.Zero)
        {
            Console.WriteLine("This is the zero IntPtr.");
        }
        else
        {
            Console.WriteLine("This is not the zero IntPtr.");
        }

        // Convert to integer types (platform dependent)
        if (IntPtr.Size == 4) // 32-bit
        {
            int intValue = ptr.ToInt32();
            Console.WriteLine($"Converted to int32: {intValue}");
        }
        else if (IntPtr.Size == 8) // 64-bit
        {
            long longValue = ptr.ToInt64();
            Console.WriteLine($"Converted to int64: {longValue}");
        }
    }
}